473,385 Members | 1,766 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,385 software developers and data experts.

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 38440
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.