Click here to Skip to main content
Rate this: bad
good
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 1: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
0 DamithSL 325
1 Maciej Los 217
2 OriginalGriff 213
3 BillWoodruff 79
4 Jose Garcia Sevilla 75
0 OriginalGriff 7,953
1 DamithSL 6,139
2 Sergey Alexandrovich Kryukov 5,454
3 Maciej Los 5,293
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 30 Mar 2010
Copyright © CodeProject, 1999-2014
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