473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Simple question about converting an integer into a byte

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[i] = (byte)bit_buffer;"
End result: bArray[i] = 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!
Jul 21 '06 #1
3 1517
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" <ms**@precisionport.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
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[i] = (byte)bit_buffer;"
End result: bArray[i] = 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!


Jul 21 '06 #2
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" <ch*****@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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" <ms**@precisionport.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
>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[i] = (byte)bit_buffer;"
End result: bArray[i] = 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!



Jul 21 '06 #3
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" <ch*****@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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" <ms**@precisionport.comwrote in message
news:On**************@TK2MSFTNGP03.phx.gbl...
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[i] = (byte)bit_buffer;"
End result: bArray[i] = 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!


Jul 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
2
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some...
5
by: redeagle | last post by:
In VB6, the code for a structure is Structure zFuheader Dim Id1(80) As Byte Dim Id2(80) As Byte End Structure However, in .NET, it doesn't let you declare the array size in the structure.
5
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
9
by: Jeremy Kitchen | last post by:
Are there any library functions that can help me to do this? If necessary I can convert the string to a byte array. I don't want to have to write my own Hex conversion if it isn't necessary. ...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.