Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is the converting the int value to byte value
Posted

It's not possible. A byte is 0 to 255. An int is a whole lot bigger than that. So, you can convert an int to a stream of bytes, but not to a byte.
 
Share this answer
 
Use Convert.ToByte(intValue) which will convert your integer value to byte. If the entered value is too big or too small, it will through OverFlowException.

Better to use the Byte.TryParse(...) method.
 
Share this answer
 
int intValue = 2;

byte byteValue = Convert.ToByte(intValue);

This is what you're looking for?

As indeed pointed out below a byte goes until the number 255 and then stops because a byte can holds 8 bits

binary: 11111111 = 255
 
Share this answer
 
v3
As already pointed out, generally speaking, an integer will not fit in a byte. Please post some context info (why do you need to perform such a conversion?) to get better help.
:)
 
Share this answer
 
v2
Maybe what you need is converting an integer to its byte representation? i.e. 4 bytes for 32 bits integers.

In order to do that, the framework provides the BitConverter class.

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900