C# bytes

byte data type is an unsigned 8 bit integer, has a value range 0 ~ 255. sbyte data type is an signed 8 bit integer, has a value range -128 ~ 128.

byte b=4;
byte b1=4,b2=9;

byte converts to larger storage numeric data type do not need casting, including short, ushort, int, uint, long, ulong, float, double, decimal.
byte b = 4;
float x = b; //ok
double y=b; //ok

Casting is needed when converts a larger storage sized number to byte.
int x = 220;
byte b = (byte) x;
byte b = (byte) 3 + 4; //the add result is int by default

Byte and string/char conversion.
char c = 'b';
byte b = (byte)c; //b=98
string str = "23";
byte b = System.Byte.Parse(str); //b=23
//to convert a string with caution, use TryParse
byte b;
string str="23";
if (System.Byte.TryParse(str.Trim(),out b))
{
...
}

Byte array to string.
byte[] arr = { 85, 46, 98, 101, 95 };
string str = System.Text.Encoding.ASCII.GetString(arr); //str = U.be_




endmemo.com © 2024  | Terms of Use | Privacy | Home