Functions related to Byte transformation in C

  • 2021-09-20 21:21:44
  • OfStack

1. Convert an object to an byte object


public static byte GetByte(object o)
{
byte retInt = 0;
if (o != null)
{
byte tmp;
if (byte.TryParse(o.ToString().Trim(), out tmp))
{
retInt = tmp;
}
}
return retInt;
} 

2. Converts a 106-ary string to an byte object, starting with 0x


public static byte GetByteFormHex(string hexValue)
{
try
{
return Convert.ToByte(hexValue, 16);
}
catch 
{
return 0;
}
}

3. Convert a single character to an byte object


public static byte GetByteFormSingleString(string value)
{
return GetByteFormChar(Convert.ToChar(value));
}

4. Convert a string to an byte array


public static byte[] GetBytes(string values)
{
return System.Text.Encoding.Default.GetBytes(values);
}

The above content is this site to introduce C # Byte conversion related functions, I hope to help you!


Related articles: