/// <summary> /// byte[]转换成16进制字符串 /// </summary> /// <param name="bytes">需要转换的byte[]</param> /// <param name="enum16"></param> /// <returns></returns> public static string BytesTo16(byte[] bytes, Enum16进制隔离符 enum16) { string outString = ""; for (int i = 0; i < bytes.Length; i++) { if (bytes[i].ToString("X").Length < 2) //16进制数为一位时前面填充0 { outString += "0" + bytes[i].ToString("X") + AddGeLi(enum16); //转成16进制数据 } else// { outString += bytes[i].ToString("X") + AddGeLi(enum16);//转成16进制数据 } } return(outString); }
/// <summary> /// 字符串转换成16进制 /// </summary> /// <param name="inSting"></param> /// <param name="enum16"></param> /// <returns></returns> public static string StringTo16(string inSting, Enum16进制隔离符 enum16) { string outString = ""; byte[] bytes = Encoding.Default.GetBytes(inSting); for (int i = 0; i < bytes.Length; i++) { int strInt = Convert.ToInt16(bytes[i] - '\0'); string s = strInt.ToString("X"); if (s.Length == 1) { s = "0" + s; } s = s + AddGeLi(enum16); outString += s; } return(outString); }
/// <summary> /// 把Enum16进制隔离符转换成实际的字符串 /// </summary> /// <param name="enum16">Enum16进制隔离符</param> /// <returns></returns> private static string AddGeLi(Enum16进制隔离符 enum16) { switch (enum16) { case Enum16进制隔离符.无: return(""); case Enum16进制隔离符.Ox: return("0x"); case Enum16进制隔离符.OX: return("0X"); case Enum16进制隔离符.空格: return(" "); default: return(""); } }
/// <summary> /// 把byte[]直接转换成字符串,直接以2进制形式显示出来。 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static string BytesTo2String(byte[] bytes, Enum16进制隔离符 enum16) { string outString = ""; for (int i = 0; i < bytes.Length; i++) { string tempString = Convert.ToString(bytes[i], 2); if (tempString.Length != 8) { string add0 = ""; for (int j = 0; j < 8 - tempString.Length; j++) { add0 += "0"; } outString += add0 + tempString + AddGeLi(enum16); } else { outString += tempString + AddGeLi(enum16); } } return(outString); }
/// <summary> /// 把byte[]转换成String /// </summary> /// <param name="bytes">需要转换的byte[]</param> /// <param name="enum16">隔离符</param> /// <returns></returns> public static string BytesToString(byte[] bytes, Enum16进制隔离符 enum16) { return(_16ToString(BytesTo16(bytes, enum16))); }
private static string AddGeLi(Enum16进制隔离符 enum16) { switch (enum16) { case Enum16进制隔离符.无: return ""; case Enum16进制隔离符.Ox: return "0x"; case Enum16进制隔离符.OX: return "0X"; case Enum16进制隔离符.空格: return " "; default: return ""; } }
public static string StringTo16(string inSting, Enum16进制隔离符 enum16) { string outString = ""; byte[] bytes = Encoding.Default.GetBytes(inSting); for (int i = 0; i < bytes.Length; i++) { int strInt = Convert.ToInt16(bytes[i] - '\0'); string s = strInt.ToString("X"); if (s.Length == 1) { s = "0" + s; } s = s + AddGeLi(enum16); outString += s; } return outString; }