using System; namespace HC { public class HCConvertUtils { public static string BytesToHexString(byte[] bytes, bool isUpperCase) { if (bytes == null) return ""; char[] hexDigits = isUpperCase ? HEX_DIGITS_UPPER : HEX_DIGITS_LOWER; int len = bytes.Length; if (len == 0) return ""; char[] ret = new char[len << 1]; for (int i = 0, j = 0; i < len; i++) { ret[j++] = hexDigits[bytes[i] >> 4 & 0x0f]; ret[j++] = hexDigits[bytes[i] & 0x0f]; } return new string(ret); } public static byte[] HexStringToBytes(string hexString) { if (IsSpace(hexString)) return new byte[0]; int len = hexString.Length; if (len % 2 != 0) { hexString = "0" + hexString; len = len + 1; } char[] hexBytes = hexString.ToUpper().ToCharArray(); byte[] ret = new byte[len >> 1]; for (int i = 0; i < len; i += 2) { ret[i >> 1] = (byte)(HexToDec(hexBytes[i]) << 4 | HexToDec(hexBytes[i + 1])); } return ret; } public static bool IsSpace(string s) { if (s == null) return true; for (int i = 0, len = s.Length; i < len; ++i) { if (!char.IsWhiteSpace(s[i])) { return false; } } return true; } private static int HexToDec(char hexChar) { if (hexChar >= '0' && hexChar <= '9') { return hexChar - '0'; } else if (hexChar >= 'A' && hexChar <= 'F') { return hexChar - 'A' + 10; } else { throw new ArgumentException(); } } private static readonly char[] HEX_DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; private static readonly char[] HEX_DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; } }