How do I convert an array of bytes to an unsigned char (0 to 255, not -127 to 127)
If I'm not mistaken BitConverter
only has BitConverter.ToChar
which doesn't give me the result I'm looking for.
Thank you.
How do I convert an array of bytes to an unsigned char (0 to 255, not -127 to 127)
If I'm not mistaken BitConverter
only has BitConverter.ToChar
which doesn't give me the result I'm looking for.
Thank you.
An array of bytes (byte[]
) is already an array of items that are 0 to 255. char
s in .NET are multibyte because they are Unicode. There's no such thing as a uchar
in .NET.
It depends on your usage. BitConverter.ToChar
is used to convert 2 bytes from your byte array to an Unicode character (think of that as deserialization). In .NET, the char
data type is used to represent an actual Unicode character, so it contains 2 bytes.
If your goal is to simply get an array of integers between 0 and 255, your byte array is exactly what you need.
You can convert the byte array to string
string str = System.Text.Encoding.ASCII.GetString(bytes);
or cast/convert them to char
:
char[] chars = Array.ConvertAll(bytes, Convert.ToChar);