??problems typecasting in VB.NET??

W

weg22

Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text > 32767, I get a run-time error. Looking for some
suggestions.

Thanks in advance,
-weg
 
A

Armin Zingler

Hi all,

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text > 32767, I get a run-time error. Looking for
some suggestions.

I'm not sure what you are trying to do. If you want to input values > 32767,
why not use a different data type?


Armin
 
W

weg22

I'm not sure what you are trying to do. If you want to input values > 32767,
why not use a different data type?
I'm not sure what you are trying to do. If you want to input values > 32767,
why not use a different data type?

Armin

To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt > 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't have
to call a function like above every time I read in a value. I would
rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.
 
G

Guest

To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt > 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't have
to call a function like above every time I read in a value. I would
rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.


Casting in VB usually implies a conversion. I assume from your original
that you want to do something like sx=(SHORT)ux from the good/bad old days
which does no conversion. The CShort() call is failing because it can't
convert (not cast) 65000 into a Short.

If you want economy and legibility of source code, call a function.

If you really want to do an old C style cast in VB, then you probably have
to make the equivalent of a union, eg


<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> Public Structure Union
<Runtime.InteropServices.FieldOffset(0)> Public xShort As Short
<Runtime.InteropServices.FieldOffset(0)> Public xUShort As UShort
<Runtime.InteropServices.FieldOffset(0)> Public xInteger As Integer
<Runtime.InteropServices.FieldOffset(0)> Public xIntPtr As IntPtr
<Runtime.InteropServices.FieldOffset(0)> Public xByte As Byte
etc as with as much as you want.....
End Structure

Then you can have one of these things laying around, eg
Dim z as Union
and cast like this
dim u as UShort
dim s as Short = whatever
z.xShort = s : u = z.xUShort ' no function calls, but two assignments

I personally don't like this kind of thing in VB, and I think the function
call is the better answer. But I think this gets you as close as possible in
VB to the cast you are looking for.
 
A

Armin Zingler

To convert from an unsigned int to a signed int, the code would read
something like this:

If unsignedInt > 32767 Then
signedInt = unsignedInt - 65535
Else
signedInt = unsignedInt
End If

I would like to do the above conversion by typecasting so I don't
have to call a function like above every time I read in a value. I
would rather do something like:

signedInt = CShort(unsignedInt)

but I get an overflow error.

/Casting/ is not possible because casting must be type safe (Short and
UShort are not derived from each other). /Converting/ is possible, either
they way you did above, or use the BitConverter class, as suggested by
Alexander before (now that I know what you are trying I also
suggest it).

Dim s As Short
Dim u As UShort = 40000
s = BitConverter.ToInt16(BitConverter.GetBytes(u), 0)

But I guess your own function is faster.

In any case, you should enable Option Strict On.

Shared Function UShortToShort(ByVal u As UShort) As Short
If u > 32767 Then
Return CShort(CInt(u) - 65536)
Else
Return CShort(u)
End If
End Function


Armin
 
P

Patrice

This is expected as the max value for a short is 32767. So you'll never be
able to "convert" a ushort value that is 40000 into a short value that would
have the same absolute value (if you meant the internal representation
you'll get a negative value and I'm not sure this is what you want as this
could lead to wrong condition for whatever calculation you would do on this)

For now, possible options would be :
- use Ushort variables
- use a wider datatype such as integer so that you can store a value such as
40000 that you can't store as such in a Short
- the last resort would be to use the same storage pattern but then the
value will be negative so it really depends what you are trying to do and
could be risky if you have some computation later in your code...
 
H

Herfried K. Wagner [MVP]

I'm having problems trying to type cast in VB.Net. I'd like to read
in a 16 bit unsigned integer and convert it to a signed 16 bit
integer. I'm using the following line of code:

Dim wordVal As Short

wordVal = CShort(Val(input.Text))
output.Text = wordVal

Whenever input.Text > 32767, I get a run-time error. Looking for some
suggestions.

'Short' is a signed data type ('Int16'). You may want to use 'UShort'
instead. I suggest to use 'UShort.TryParse' to parse the user input instead
of 'CShort(Val(...))'.
 
G

Guest

You can not convert all unsigned int16s to signed int16s
An unsigned int16 has 16 bits of data, a signed int16 has a sign bit and 15
bits of data, so it will not fit.
You will have to convert it to an int32/integer

Guy
 
P

Phill W.

Dim wordVal As Short
wordVal = CShort(Val(input.Text))
Whenever input.Text > 32767, I get a run-time error.
Looking for some suggestions.

Well; what do you know?
The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
back again still works!

Dim unsigned as UInt16 = 40000
Dim signed as Int16 _
= CShort(Val("&H" & Hex(unsigned)))

HTH,
Phill W.
 
G

Guest

Yes Phil, but 40000 then becomes -25536 :)

Guy

Phill W. said:
Well; what do you know?
The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
back again still works!

Dim unsigned as UInt16 = 40000
Dim signed as Int16 _
= CShort(Val("&H" & Hex(unsigned)))

HTH,
Phill W.
 
P

Phill W.

guy said:
but 40000 then becomes -25536 :)

Exactly what I would expect.

Read up about the Short Data Type and its limitations.

You can't fit a quart into a pint pot; if it holds the pint, it's doing
the best it can...

Regards,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top