class ByteUtil { /// /// converts bytedata in a byte array to a hexadecimal string /// public static string Byte2String(byte[] b, int offset, int len, string separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.AppendFormat("{0,1:X2}", b[i+offset]); sb.Append(separator); } return sb.ToString(); } //converts hexadecimal string to a byte array public static byte[] String2Byte(string nstr) { string str = (string)nstr.Clone(); str = str.Replace(" ", ""); str = str.Replace("\t", ""); int lenByte = str.Length / 2; byte[] ret = new byte[lenByte]; if ((str.Length % 2) != 0) { throw new FormatException(); } if ( str.Length == 0) { throw new FormatException(); } for (int i = 0; i < lenByte; i++) { string strByte = str.Substring(i * 2, 2); ret[i] = Convert.ToByte(strByte, 16); } return ret; } }