privatestaticuint[] CreateLookup32() { var result = newuint[256]; for (int i = 0; i < 256; i++) { string s = i.ToString("X2"); result[i] = ((uint)s[0]) + ((uint)s[1] << 16); } return result; }
publicstaticstringByteArrayToHexViaLookup32(byte[] bytes) { uint[] lookup32 = _lookup32; char[] result = newchar[bytes.Length * 2]; for (int i = 0; i < bytes.Length; i++) { uint val = lookup32[bytes[i]]; result[2 * i] = (char)val; result[2 * i + 1] = (char)(val >> 16); } returnnewstring(result); }
// this method is from http://stackoverflow.com/a/17923942/2753545 publicstaticbyte[] HexToByteUsingByteManipulation(string s) { byte[] bytes = newbyte[s.Length / 2]; for (int i = 0; i < bytes.Length; i++) { int hi = s[i * 2] - 65; hi = hi + 10 + ((hi >> 31) & 7);
int lo = s[i * 2 + 1] - 65; lo = lo + 10 + ((lo >> 31) & 7) & 0x0f;