Simple question about converting an integer into a byte

N

news.microsoft.com

Hello,

I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue.
Steping through both the C# and VB.NET apps i get identical results but for
one section.

Notes: bit_buffer = 360 and bArray is an array of bytes going into these
lines of code

**********
(C#):
"bArray = (byte)bit_buffer;"
End result: bArray = 104
(VB.NET):
bArray(i) = CByte(bit_buffer)
End Result: Overflow because 360 can't fit into a byte of max 255
***********

My question is why does the C# version convert it into a byte value of 104
when the VB one doesn't??

Thanks in advance!
 
C

chanmm

I am surprised why VB.NET not 104. Basically a byte is only 8 bit so the
maximum number it can handle only 265. 104 is basically 306 - 256 = 104.

chanmm
 
K

Kevin Spencer

Actually, the maximum value of a byte is 255. A byte can contain 256
possible values, but the first is 0, not 1.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.


chanmm said:
I am surprised why VB.NET not 104. Basically a byte is only 8 bit so the
maximum number it can handle only 265. 104 is basically 306 - 256 = 104.

chanmm

news.microsoft.com said:
Hello,

I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue.
Steping through both the C# and VB.NET apps i get identical results but
for
one section.

Notes: bit_buffer = 360 and bArray is an array of bytes going into these
lines of code

**********
(C#):
"bArray = (byte)bit_buffer;"
End result: bArray = 104
(VB.NET):
bArray(i) = CByte(bit_buffer)
End Result: Overflow because 360 can't fit into a byte of max 255
***********

My question is why does the C# version convert it into a byte value of
104
when the VB one doesn't??

Thanks in advance!

 
N

news.microsoft.com

Thanks guys. after looking into it after what you said i noticed C# strips
the extra bits to make it a byte and VB does not. VB makes you do the
striping yourself.

for example:

int 360 converted to bits is 101101000 (has one extra bit)

when i convert to a byte in C# i get 01101000 which is 8 bits and equals 104

VB.NET simply doesn't do it. you have to convert it into a byte array using
System.BitConverter and select the first element or write a procedure to
strip the extra bits.

cheers!

-king

chanmm said:
I am surprised why VB.NET not 104. Basically a byte is only 8 bit so the
maximum number it can handle only 265. 104 is basically 306 - 256 = 104.

chanmm

news.microsoft.com said:
Hello,

I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue.
Steping through both the C# and VB.NET apps i get identical results but
for
one section.

Notes: bit_buffer = 360 and bArray is an array of bytes going into these
lines of code

**********
(C#):
"bArray = (byte)bit_buffer;"
End result: bArray = 104
(VB.NET):
bArray(i) = CByte(bit_buffer)
End Result: Overflow because 360 can't fit into a byte of max 255
***********

My question is why does the C# version convert it into a byte value of 104
when the VB one doesn't??

Thanks in advance!

 

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