JRB

Hi, I have an array of 200 bytes. I want to iterate through each element and convert it to an SByte (which I understand is really an 8-byte signed integer). I can't find a good method to do this. If my byte value is > 127, for instance 253, I keep getting an overflow error. I need a method to just stuff the bits of my byte into the SByte. Any help would be appreciated. Thanks.

JRB



Re: Visual Basic Language Converting to SByte

kleinma

SBYTE is not an 8-byte integer...

it is an 8 BIT (1 byte) integer that can hold a value of -128 through 127 (hence your error)

you loose some of the capacity because it needs that to indicate the positive or negative. That is why SByte can hold -128 through 127 while a regular byte can hold 0 to 255






Re: Visual Basic Language Converting to SByte

decyclone

In your loop, while converting to SByte, check if the original value is > 127 just store 127. If you just want to convert from Byte to SByte, store SByteValue = (ByteValue - 128).





Re: Visual Basic Language Converting to SByte

JRB

Sorry, my mistake, I meant to say SByte is an 8-bit signed integer, not 8 byte signed integer.

If the value of my byte is for instance, 253, this should correspond to -3 when I convert to SByte. So how do I get this conversion to work Everything I've tried gives me an overflow. (253 = 1111 1101, which when signed is -3). Thanks.

JRB





Re: Visual Basic Language Converting to SByte

DMan1

The convert class has several methods and overloads for doing type conversions:

http://msdn2.microsoft.com/en-us/library/system.convert.aspx

Dim MySbyte As SByte = Convert.ToSByte(TheByte)

Edit: as noted if you try and do a conversion of a byte greater than 127 you will get an overflow exception






Re: Visual Basic Language Converting to SByte

kleinma

DMan1 wrote:

The convert class has several methods and overloads for doing type conversions:

http://msdn2.microsoft.com/en-us/library/system.convert.aspx

Dim MySbyte As SByte = Convert.ToSByte(TheByte)

That will still overflow if TheByte is over the size that SByte can handle (127)

I am still giving thought as to how to address this issue without getting an overflow.






Re: Visual Basic Language Converting to SByte

DMan1

kleinma wrote:
DMan1 wrote:

The convert class has several methods and overloads for doing type conversions:

http://msdn2.microsoft.com/en-us/library/system.convert.aspx

Dim MySbyte As SByte = Convert.ToSByte(TheByte)

That will still overflow if TheByte is over the size that SByte can handle (127)

I am still giving thought as to how to address this issue without getting an overflow.

Yes you posted and I editted at the same time:

If the Byte value is greater than 127 then:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim thebyte As Byte = 253

Dim MySbyte As SByte

If thebyte > 127 Then

MySbyte = Convert.ToSByte(thebyte - 256)

Else

MySbyte = Convert.ToSByte(thebyte)

End If

Debug.Print(CStr(MySbyte))

End Sub






Re: Visual Basic Language Converting to SByte

JRB

Thanks. I kinda had the same thing on the go:

If (theByte > 127) Then

MySByte = ((Not theByte) + 1) * -1

Else

MySByte = theByte

Just thought there was a built in method. Thanks!

JRB