473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_buffe r;"
End result: bArray[i] = 104
(VB.NET):
bArray(i) = CByte(bit_buffe r)
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 1529
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**@precision port.comwrote in message
news:On******** ******@TK2MSFTN GP03.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_buffe r;"
End result: bArray[i] = 104
(VB.NET):
bArray(i) = CByte(bit_buffe r)
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*****@hotmai l.comwrote in message
news:%2******** ********@TK2MSF TNGP02.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**@precision port.comwrote in message
news:On******** ******@TK2MSFTN GP03.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_buffe r;"
End result: bArray[i] = 104
(VB.NET):
bArray(i) = CByte(bit_buffe r)
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.BitConve rter and select the first element or write a procedure to
strip the extra bits.

cheers!

-king

"chanmm" <ch*****@hotmai l.comwrote in message
news:%2******** ********@TK2MSF TNGP02.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**@precision port.comwrote in message
news:On******** ******@TK2MSFTN GP03.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_buffe r;"
End result: bArray[i] = 104
(VB.NET):
bArray(i) = CByte(bit_buffe r)
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
2021
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 is a 128-byte header and the rest of the file is integers in 2-byte format. What I want to do is to save the binary data into several smaller files
5
4168
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 serial ports as bytes and some of these represent strings. (Like the Packed BCD date/time stamp etc). At present I read through each Byte and convert...
2
2044
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 light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
5
2122
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
4581
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 Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an...
2
6965
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 2^14 but some enterprise IDs are now larger than that. Do I just contine the conversion on to additional bytes needed to house the OID in full? So...
9
9717
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. Thanks for any help Jeremy Kitchen
12
2108
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 unsigned char byte; // 'byte' is an 8-bit unsigned value
9
5153
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 level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.