Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static public byte[] ReadHexStringToByte(string Input)
- {
- Input = Input.ToUpper().Trim();
- if (Input.Length == 0) goto fail;
- string ValidChar = "0123456789ABCDEF";
- foreach (char character in Input)
- {
- if (!ValidChar.Contains(character)) goto fail;
- }
- if (Input.Length % 2 == 1) Input = "0" + Input;
- byte[] ResultBytes = new byte[Input.Length / 2];
- int index = 0;
- for (int i = 0; i < Input.Length; i += 2)
- {
- ResultBytes[index] = Convert.ToByte(Input.Substring(i, 2), 16);
- index ++;
- }
- return ResultBytes;
- fail:
- throw new Exception("Invalid HexString");
- }
Advertisement
Add Comment
Please, Sign In to add comment