KenM KenM - 1 year ago 106
C# Question

converting a nibble to ist ascii value

I need a solution for converting each nibble of a byte to its ASCII equivalent. So given:

varA = 0xab;
char varB = ASCii value of the upper nibble of 0xab 'a' (61)
char varC = ASCii value of the lower nibble of 0xab 'b' (62)

thanks ahead of time

Answer Source

Just convert it to hex-string and encode it using ASCII:

  byte varA = 0xAB;
  byte[] result = Encoding.ASCII.GetBytes(varA.ToString("x2"));
  // result[0] is 0x61
  // result[1] is 0x62
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download