TizzyT

ReadHexStringToByte -TizzyT

Feb 19th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1.         static public byte[] ReadHexStringToByte(string Input)
  2.         {
  3.             Input = Input.ToUpper().Trim();
  4.             if (Input.Length == 0) goto fail;
  5.             string ValidChar = "0123456789ABCDEF";
  6.             foreach (char character in Input)
  7.             {
  8.                 if (!ValidChar.Contains(character)) goto fail;
  9.             }
  10.             if (Input.Length % 2 == 1) Input = "0" + Input;
  11.             byte[] ResultBytes = new byte[Input.Length / 2];
  12.             int index = 0;
  13.             for (int i = 0; i < Input.Length; i += 2)
  14.             {
  15.                 ResultBytes[index] = Convert.ToByte(Input.Substring(i, 2), 16);
  16.                 index ++;
  17.             }
  18.             return ResultBytes;
  19.         fail:
  20.             throw new Exception("Invalid HexString");
  21.         }
Advertisement
Add Comment
Please, Sign In to add comment