Convert sbyte to byte

G

Guest

How do I convert an sbyte to a byte. When I try the code
below, it blows up if the sbyte is a negative number.

sbyte[] sbytes = input;

for( int i = 0; i < sbytes.Length; i++ )
{
byte x = Convert.ToByte( sbytes );
Console.WriteLine( x );
}
 
J

Jon Skeet [C# MVP]

How do I convert an sbyte to a byte. When I try the code
below, it blows up if the sbyte is a negative number.

sbyte[] sbytes = input;

for( int i = 0; i < sbytes.Length; i++ )
{
byte x = Convert.ToByte( sbytes );
Console.WriteLine( x );
}


Rather than using Convert.ToByte, just cast the sbyte to a byte. That
will work so long as you don't have overflow checking turned on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top