KenM KenM - 2 months ago 10
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


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