C# 字节数组和十六进制字符串之间转换的另类写法
今天从http://www.cnblogs.com/NanaLich/archive/2012/05/24/2516860.html看到的,记录下来
主要是XmlSerializationReader和XmlSerializationWriter两个抽象类中包含了很多受保护的方法,其中比较有用的就是byte[]和hexString的转换,分析源码发现是由两个内部类来实现的:BinHexEncoder和BinHexDecoder,看名字就非常清楚了,专门用来处理byte[]和hex的转换的,至于为何仅仅是内部类,还不清楚原因。
class dummyWriter : XmlSerializationWriter
{
protected override void InitCallbacks() { }
public static string BytesToHex(byte[] bin)
{
return FromByteArrayHex(bin);
}
}
class dummyReader : XmlSerializationReader
{
protected override void InitCallbacks() { }
protected override void InitIDs() { }
public static byte[] HexToBytes(string hex)
{
return ToByteArrayHex(hex);
}
}