views:

75

answers:

4

I have just converted my c# code to vb.net. And it is giving exception.

c# code :

private static short[] muLawToPcmMap;

decoded[2 * i + 1] = (byte)(muLawToPcmMap[data[i]] >> 8);

vb code :

decoded(2 * i + 1) = CByte(muLawToPcmMap(data(i)) >> 8)

Exception :

Arithmetic operation resulted in an overflow.

I am in very much trouble. Please help.

A: 

It's most likely that the number you are trying to convert is outside the range of Byte or Short

Reference: link text

msarchet
But it working perfectly in c#
Barun
Please try to understand I know c# but dont know vb.net. I did not even imagine conversion will cause a problem.
Barun
please give me a solution what will be the converted code in vb of this code [decoded[2 * i + 1] = (byte)(muLawToPcmMap[data[i]] >> 8);]
Barun
Conversions can cause all kinds of problems. If I convert a BigInteger into a Int16 I can cause overflow. I would guess that it is probably on the `CShort` method. Let me write some code out to see what is actually happening.
msarchet
A: 

I notice that your C# code doesn't contain a cast to short, but your VB.NET code does. It's possible the cast is causing a shift of a bit into a bit that's causing an overflow. (Can't really tell without seeing the data.) Remove it.

Mike Hofer
actually when i converted it with a converter the casting was not there. And it was giving same exception then too.
Barun
Please please reply. I am in very much trouble.
Barun
Can you provide samples of the data you're trying to convert?
Mike Hofer
yeah sure : public static void MuLawDecode(byte[] data, out byte[] decoded) { int size = data.Length; decoded = new byte[size * 2]; for (int i = 0; i < size; i++) { decoded[2 * i] = (byte)(muLawToPcmMap[data[i]] decoded[2 * i + 1] = (byte)(muLawToPcmMap[data[i]] >> 8); } }
Barun
Hi, is there any posibility that it taking 8 as integer ??
Barun
What is the range of values for the original data?
Mike Hofer
I just tested it. It giving exception after looping 8192 times.
Barun
+1  A: 

Check if your translation of

byte[] decoded = new byte[size*2]; 

is

Dim decoded As Byte() = New Byte(size * 2 - 1) {}

or not, as in vb.net, you declare arrays with the index of the last element, not the size itself.

Depending on how you translated decoded, you have to check the rest of the code to adapt it to the version you chose.

Matthieu
Yes the converter changed it as you mentioned. But in each case it giving exception.
Barun
+5  A: 

Your code is resulting in an overflow for the data type you're working with.

The default behavior of VB.NET is to check arithmetic operations and in C# it is to not check arithmetic operations.

Add a checked statement around your C# code to see it fail also.

checked {
   // do all your work here, any overflow will cause an exception
}

Fix your code to stop overflowing. As my comments below mention, an arithmetic overflow is not something to be ignored necessarily. You're performing operations here that result in likely unexpected results and you should code explicitly for this (by increasing the size of your type or handling the failure).

The absolute last thing you should (IMO) do is under your project properties, Compile tab, Advanced Compiler Settings button, is check the checkbox labeled "Remove integer overflow checks". I personally think it's a bad idea and personally I use checked in C# whenever I do things that will overflow my variables. Fail early, fail often and all.

Adam Sills
I need to fix the vb.net code.
Barun
Yes you do. /Fix it to stop overflowing/. I mention the checked keyword because you're indicating they don't behave the same.
Adam Sills
Please dont mind. But you are not helping me.
Barun
Hi as you told me I checked it. Its giving arithmetic exception as well. But what is the solution ?
Barun
Your solution is to stop willy-nilly shifting bits and make sure your code functions. If the result of the bit-shift would overflow your variable, handle that scenario. Or make your variable type larger. The question is, what do you *want* to happen when it overflows? An overflow is one of those silent failure program doesn't respond like it should scenarios. So you handle that scenario in code.
Adam Sills
If you want an overflow to result in a value of zero, catch an exception and set the value to zero. If you want it to roll over like default C# does (likely a bad idea), use a larger variable type, bit shift that and if the resulting value is bigger than the desired data type, figure out what value you want it to be.
Adam Sills
FYI - updated my answer with a way to be lazy and not actually fix the code. I don't suggest doing it though.
Adam Sills
Actually I am doing u-law decompression. You see my c# code is running fine. problem is in here CByte(). But main confusing part is - it is giving exception after 8192 iteration. How can it possible ??
Barun
Like my answer above says: if you want it to run like C# does, remove integer overflow checks. It'll use the exact same IL to bitshift as C# does by default, so it won't check your arithmetic for overflows.
Adam Sills
Also: you're seeing it fail because since you're bit shifting a byte by an Integer, it's converting the left half of the expression to an Integer, then bit shifting it. The reason you had to put a cast in there is because neither VB.NET (nor C#) would let you do the assignment directly. So you end up taking a Byte, converting it to an Integer, bit shifting it, then try to assign it back to a Byte, at which point it's completely invalid and you get an overflow.
Adam Sills
I need to take a integer value into a byte var .. Please tell me how to do it ?
Barun
Do you want your VB.NET code to perform exactly like your C# code? If yes, see the last paragraph in my answer. If no, it's up to you to see how this algorithm you're implementing is supposed to function on data overflow. If it is to roll over, see my last paragraph above. You're already taking "a integer value into a byte var" - that's your CByte above.
Adam Sills
this is a short value that I shifting not a integer
Barun
Please just tell me how I can take last 8 bits from a integer or short. It will be very helpful information for me.
Barun
I think doing [and] with 000000011111111 . Is not it ? How can I do it ?
Barun
"value AND 255" in VB.NET will "give you the last 8 bits".
Adam Sills
Though again, that's not what your C# code is doing, which is what you based this whole question on (you said your C# code works perfectly according to another comment you made).
Adam Sills
yeah... u r right... my code is not working after and operation... please tell me solution... should I give u the c# code ?
Barun
Hi, Problem is solved. I did things as you told. Thanks. You are genius.
Barun
End of day so I missed your last comment, but glad it worked out for you.
Adam Sills