using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FiestaLib.Util { static class ByteUtils { /// /// Converts a hex-string to byte array. /// /// Hexadecimal string. /// Byte array object from string. public static byte[] HexToBytes(string pValue) { StringBuilder builder = new StringBuilder(); foreach (char c in pValue.Where(IsHexDigit).Select(Char.ToUpper)) { builder.Append(c); } string hexString = builder.ToString(); if (hexString.Length % 2 == 1) { throw new InvalidOperationException("There is an odd number of hexadecimal digits in this string."); } byte[] bytes = new byte[hexString.Length / 2]; for (int i = 0, j = 0; i < bytes.Length; i++, j += 2) { string byteString = String.Concat(hexString[j], hexString[j + 1]); bytes[i] = HexToByte(byteString); } return bytes; } /// /// Creates a hex-string from byte array. /// /// Input bytes. /// String that represents the byte-array. public static string BytesToHex(byte[] bytes, string header = "") { StringBuilder builder = new StringBuilder(header); foreach (byte c in bytes) { builder.AppendFormat("{0:X2} ", c); } return builder.ToString(); } /// /// Checks if a character is a hexadecimal digit. /// /// The character to check /// true if is a hexadecimal digit; otherwise, false. public static bool IsHexDigit(char c) { if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f')) { return true; } return false; } /// /// Convert a 2-digit hexadecimal string to a byte. /// /// The hexadecimal string. /// The byte representation of the string. private static byte HexToByte(string hex) { if (hex == null) throw new ArgumentNullException("hex"); if (hex.Length == 0 || 2 < hex.Length) { throw new ArgumentOutOfRangeException("hex", "The hexadecimal string must be 1 or 2 characters in length."); } byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber); return newByte; } } }