【总结】字节与字符串 扩展函数
public static class Ext {public static string ToHexString(this byte[] hex)
{
if (hex == null) return null;
if (hex.Length == 0) return string.Empty;
var s = new StringBuilder();
foreach (byte b in hex) {
s.Append(b.ToString("x2"));
}
return s.ToString();
}
public static byte[] ToHexBytes(this string hex)
{
if (hex == null) return null;
if (hex.Length == 0) return new byte;
int l = hex.Length / 2;
var b = new byte;
for (int i = 0; i < l; ++i) {
b = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return b;
}
} 还不错。 抛砖引玉。
页:
[1]