Click here to Skip to main content
12,405,932 members (46,409 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
this is the converting the int value to byte value
Posted 30-Mar-10 0:05am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
Smile | :)
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 30 Mar 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100