c# - Convert a hex string (byte list) to a byte array -


i want convert string ("00812b1fa4bea310199d6e00dd010f5402000001807") byte array. want each digit of string hex value.

expected result:

array[0] = 0x00;  array[1] = 0x81;  array[0] = 0x2b;  array[0] = 0x1f;  etc... 

i tried several methods. none gave expected result. closest ones were:

private static byte[] convert(string tt) {     byte[] bytes2 = new byte[tt.length];     int = 0;     foreach ( char c in tt)     {         bytes2[i++] = (byte)((int)c - 48);     }     return bytes2; }  public static byte[] converthexstringtobytearray(string hexstring) {      byte[] hexasbytes = new byte[hexstring.length / 2];     (int index = 0; index < hexasbytes.length; index++)     {         string bytevalue = hexstring.substring(index * 2, 2);         byte[] =  getbytes(bytevalue);         hexasbytes[index] = a[0];     }     return hexasbytes; } 

found solution here: how convert byte array hexadecimal string, , vice versa? ( answer starting inverse function waleed eissa code) difficult find because thread has 36 answers.

 public static byte[] hextobytes(string hexstring)     {         byte[] b = new byte[hexstring.length / 2];         char c;         (int = 0; < hexstring.length / 2; i++)         {             c = hexstring[i * 2];             b[i] = (byte)((c < 0x40 ? c - 0x30 : (c < 0x47 ? c - 0x37 : c - 0x57)) << 4);             c = hexstring[i * 2 + 1];             b[i] += (byte)(c < 0x40 ? c - 0x30 : (c < 0x47 ? c - 0x37 : c - 0x57));         }          return b;     } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -