473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Integer to Byte

I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as
the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c#
program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?
Nov 20 '05 #1
9 38459
Well, it seems like the VB.Net way is technically correct, insofar as you
can't (correctly) cast a number greater than 255 to a byte. But if you're
just looking to extract the least-significant byte out of a longer number,
try using this formula in your code:

X Mod 256

With X being any integer.

E.g., 137694 Mod 256 = 222.
"Dante" <da******@hotmail.com> wrote in message
news:03****************************@phx.gbl...
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as
the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c#
program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?

Nov 20 '05 #2
Dante,
In addition to Robert's comments (which I would recommend doing).

Remember that by default that overflow checking is on in VB.NET while it is
off by default in C#. Hence the exception in VB.NET, while truncation in C#.

Unfortunately the option to change overflow checking is at the project
level, not file, function, or statement level, which is why I would
recommend using the Mod operator as Robert suggests.

Hope this helps
Jay

"Dante" <da******@hotmail.com> wrote in message
news:03****************************@phx.gbl...
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as
the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c#
program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?

Nov 20 '05 #3
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80
-----Original Message-----
Well, it seems like the VB.Net way is technically correct, insofar as youcan't (correctly) cast a number greater than 255 to a byte. But if you'rejust looking to extract the least-significant byte out of a longer number,try using this formula in your code:

X Mod 256

With X being any integer.

E.g., 137694 Mod 256 = 222.
"Dante" <da******@hotmail.com> wrote in message
news:03****************************@phx.gbl...
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c# program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?

.

Nov 20 '05 #4
Dante,
Then you will need to 'normalize' it with a method other than the Mod
operator. (repeated subtraction or addition comes to mind).

Or keep that module in C#.

Or turn off the overflow checking.

Hope this helps
Jay

"Dante" <da******@hotmail.com> wrote in message
news:04****************************@phx.gbl...
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80
-----Original Message-----
Well, it seems like the VB.Net way is technically

correct, insofar as you
can't (correctly) cast a number greater than 255 to a

byte. But if you're
just looking to extract the least-significant byte out of

a longer number,
try using this formula in your code:

X Mod 256

With X being any integer.

E.g., 137694 Mod 256 = 222.
"Dante" <da******@hotmail.com> wrote in message
news:03****************************@phx.gbl...
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c# program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?

.

Nov 20 '05 #5
I figured it out, instead of using mod 256, if I AND the
integer with 255 it works

-10471344 AND 255 = 80

Thanks for your help!
-----Original Message-----
Dante,
Then you will need to 'normalize' it with a method other than the Modoperator. (repeated subtraction or addition comes to mind).
Or keep that module in C#.

Or turn off the overflow checking.

Hope this helps
Jay

"Dante" <da******@hotmail.com> wrote in message
news:04****************************@phx.gbl...
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80
>-----Original Message-----
>Well, it seems like the VB.Net way is technically

correct, insofar as you
>can't (correctly) cast a number greater than 255 to a

byte. But if you're
>just looking to extract the least-significant byte out of
a longer number,
>try using this formula in your code:
>
>X Mod 256
>
>With X being any integer.
>
>E.g., 137694 Mod 256 = 222.
>
>
>"Dante" <da******@hotmail.com> wrote in message
>news:03****************************@phx.gbl...
>> I'm converting a C# program to VB.net, but i'm having
>> problems converting a integer to a byte in the same
way as
>> the c# program does.
>>
>> //C# program
>> int i = 137694;
>> byte b = (byte) i;
>> //b returns as value 222
>>
>> 'VB program
>> dim i as integer = 137694
>> dim b as byte = CByte(i)
>> 'b returns Overflow Error
>>
>> I understand that the CByte function only takes
values >> from 0 - 255, which is why the overflow error, but the c#
>> program casts from int to byte and gets a value of

222. >> Anyone know how to get my VB.net app to do this?
>
>
>.
>

.

Nov 20 '05 #6
Good catch -- I didn't consider negative numbers as a possible input.

I noticed that 256 - 176 = 80. So, a quick fix is to use an if/then to test
whether the input is positive or negative. If it's negative, use the
following foruma:

256 + Abs (X Mod 256)

For example, if X is -10471344, it returns 80. X Mod 256 is a negative
number, so it's evaluated as 256 + (-176)

There might be a more elegant mathematical solution, but this should get the
job done.

"Dante" <da******@hotmail.com> wrote in message
news:04****************************@phx.gbl...
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80
-----Original Message-----
Well, it seems like the VB.Net way is technically

correct, insofar as you
can't (correctly) cast a number greater than 255 to a

byte. But if you're
just looking to extract the least-significant byte out of

a longer number,
try using this formula in your code:

X Mod 256

With X being any integer.

E.g., 137694 Mod 256 = 222.
"Dante" <da******@hotmail.com> wrote in message
news:03****************************@phx.gbl...
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as the c# program does.

//C# program
int i = 137694;
byte b = (byte) i;
//b returns as value 222

'VB program
dim i as integer = 137694
dim b as byte = CByte(i)
'b returns Overflow Error

I understand that the CByte function only takes values
from 0 - 255, which is why the overflow error, but the c# program casts from int to byte and gets a value of 222.
Anyone know how to get my VB.net app to do this?

.

Nov 20 '05 #7
Just saw your solution (damn slow newsservers <g>.) This looks much better.
Good idea.
"Dante" <da******@hotmail.com> wrote in message
news:1f*****************************@phx.gbl...
I figured it out, instead of using mod 256, if I AND the
integer with 255 it works

-10471344 AND 255 = 80

Thanks for your help!
-----Original Message-----
Dante,
Then you will need to 'normalize' it with a method other

than the Mod
operator. (repeated subtraction or addition comes to

mind).

Or keep that module in C#.

Or turn off the overflow checking.

Hope this helps
Jay

"Dante" <da******@hotmail.com> wrote in message
news:04****************************@phx.gbl...
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80

>-----Original Message-----
>Well, it seems like the VB.Net way is technically
correct, insofar as you
>can't (correctly) cast a number greater than 255 to a
byte. But if you're
>just looking to extract the least-significant byte out of a longer number,
>try using this formula in your code:
>
>X Mod 256
>
>With X being any integer.
>
>E.g., 137694 Mod 256 = 222.
>
>
>"Dante" <da******@hotmail.com> wrote in message
>news:03****************************@phx.gbl...
>> I'm converting a C# program to VB.net, but i'm having
>> problems converting a integer to a byte in the same way as
>> the c# program does.
>>
>> //C# program
>> int i = 137694;
>> byte b = (byte) i;
>> //b returns as value 222
>>
>> 'VB program
>> dim i as integer = 137694
>> dim b as byte = CByte(i)
>> 'b returns Overflow Error
>>
>> I understand that the CByte function only takes values >> from 0 - 255, which is why the overflow error, but the c#
>> program casts from int to byte and gets a value of 222. >> Anyone know how to get my VB.net app to do this?
>
>
>.
>

.

Nov 20 '05 #8
Dante,
Doh! forgot about anding with 255. :-)

Jay

"Dante" <da******@hotmail.com> wrote in message
news:1f*****************************@phx.gbl...
I figured it out, instead of using mod 256, if I AND the
integer with 255 it works

-10471344 AND 255 = 80

Thanks for your help!
-----Original Message-----
Dante,
Then you will need to 'normalize' it with a method other

than the Mod
operator. (repeated subtraction or addition comes to

mind).

Or keep that module in C#.

Or turn off the overflow checking.

Hope this helps
Jay

"Dante" <da******@hotmail.com> wrote in message
news:04****************************@phx.gbl...
Thanks, that works great, except for negative numbers do
not seem to work.
for example:

-10471344 mod 256 = -176

and in the c# program the value of -10471344 casted to a
byte is equal to 80

>-----Original Message-----
>Well, it seems like the VB.Net way is technically
correct, insofar as you
>can't (correctly) cast a number greater than 255 to a
byte. But if you're
>just looking to extract the least-significant byte out of a longer number,
>try using this formula in your code:
>
>X Mod 256
>
>With X being any integer.
>
>E.g., 137694 Mod 256 = 222.
>
>
>"Dante" <da******@hotmail.com> wrote in message
>news:03****************************@phx.gbl...
>> I'm converting a C# program to VB.net, but i'm having
>> problems converting a integer to a byte in the same way as
>> the c# program does.
>>
>> //C# program
>> int i = 137694;
>> byte b = (byte) i;
>> //b returns as value 222
>>
>> 'VB program
>> dim i as integer = 137694
>> dim b as byte = CByte(i)
>> 'b returns Overflow Error
>>
>> I understand that the CByte function only takes values >> from 0 - 255, which is why the overflow error, but the c#
>> program casts from int to byte and gets a value of 222. >> Anyone know how to get my VB.net app to do this?
>
>
>.
>

.

Nov 20 '05 #9
Hi Jay,

ROFL - I didn't want to say anything but I was surprised. ;-)

Regards,
Fergus
Nov 20 '05 #10

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

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
20
by: ML | last post by:
Integers are stored in tables using only 4 bytes. Is there a way in SQL to retrieve the value as it is actually stored, not converted back into the displayed number? For example, if I have...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
11
by: cmdolcet69 | last post by:
Public Shared adc_value As word_byte Public Class byte_low_hi Public low_byte As Byte Public high_byte As Byte End Class pmsg(2) = CChar(adc_value.word16.high_byte)
0
by: Atomical | last post by:
I'm writing a Gnutella client in PHP and in order to create routing tables I need to store a bit inside a byte and then send those bytes across a file stream. However, I'm not sure how to convert...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.