Search This Blog

Tuesday, February 23, 2016

Convert ushort to byte array and to ASCII code C# using BitConverter

C#  > System > BitConverter

Converts 

  • base data types to an array of bytes,
  • array of bytes to base data types.


Example

Convert ushort to ASCII code.
(ascii code 2 bytes array)


ushort number = 5678;

byte[] byteArray = BitConverter.GetBytes(number);

foreach (var result in byteArray)
{
string val = System.Convert.ToChar(result).ToString();
}