从十六进制大部分重量轻转换在C#字节? [英] Most light weight conversion from hex to byte in c#?
问题描述
可能重复:结果
的你怎么转换成字节数组十六进制字符串转换,反之亦然?
块引用>
我需要一个高效,快捷的方式做到这一点的转换。我已经尝试了两种不同的方式,但他们不是为我足够的效率。是否有任何其他快速的方法来完成这一个实时的方式与大数据的应用程序?
公共字节[] StringToByteArray(串十六进制)
{
返回Enumerable.Range(0,hex.Length / 2)。选择(X => Byte.Parse(hex.Substring(2 * X,2),的NumberStyles .HexNumber))ToArray的()。
}
上面的人觉得更有效的给我。
公共静态的byte [] stringTobyte(字符串十六进制串)
{
试
{
INT bytesCount =( hexString.Length)/ 2;
字节[]字节=新的字节[bytesCount]
表示(中间体X = 0; X&下; bytesCount ++ x)的
{
字节[X] = Convert.ToByte(hexString.Substring(X * 2,2),16 );
}
返回字节;
}
抓
{
扔;
}
解决方案我把基准代码问题,并重新设计它来测试十六进制来这里给出字节方式:
HexToBytesJon:36979.7平均蜱(超过150运行)
HexToBytesJon2:35886.4平均蜱(超过150运行)
HexToBytesJonCiC:31230.2平均蜱(超过150运行)
HexToBytesJase:15359.1平均蜱(超过150运行)
HexToBytesJon
是的的乔恩的第一个版本,而HexToBytesJon2
是第二个变种。
HexToBytesJonCiC
是乔恩的版本 CodesInChaos 的建议码。
HexToBytesJase
是我的尝试,同时基于上述而是替代半字节转换而避开错误检查,和分支:公共静态的byte [] HexToBytesJase(字符串十六进制串)
{
如果((hexString.Length&安培;!1)= 0)
{
抛出新的ArgumentException(输入必须有偶数个字符);
}
字节[] = RET新字节[hexString.Length / 2];
的for(int i = 0; I< ret.Length;我++)
{
INT高=十六进制串[我* 2];
INT低=十六进制串[I * 2 + 1];
=高(高&安培; 0xF的)+((高&安培; 0X40)GT;> 6)* 9;
=低(低速& 0xF的)+((低速& 0X40)GT;> 6)* 9;
RET [I] =(字节)((高<< 4)|低);
}
返回RET;
}
Possible Duplicate:
How do you convert Byte Array to Hexadecimal String, and vice versa?I need an efficient and fast way to do this conversion. I have tried two different ways, but they are not efficient enough for me. Is there any other quick method to accomplish this in a real-time fashion for an application with huge data?
public byte[] StringToByteArray(string hex) { return Enumerable.Range(0, hex.Length / 2).Select(x => Byte.Parse(hex.Substring(2 * x, 2), NumberStyles.HexNumber)).ToArray(); }
The above one felt more efficient to me.
public static byte[] stringTobyte(string hexString) { try { int bytesCount = (hexString.Length) / 2; byte[] bytes = new byte[bytesCount]; for (int x = 0; x < bytesCount; ++x) { bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16); } return bytes; } catch { throw; }
解决方案I took the benchmarking code from the other question, and reworked it to test the hex to bytes methods given here:
HexToBytesJon: 36979.7 average ticks (over 150 runs) HexToBytesJon2: 35886.4 average ticks (over 150 runs) HexToBytesJonCiC: 31230.2 average ticks (over 150 runs) HexToBytesJase: 15359.1 average ticks (over 150 runs)
HexToBytesJon
is Jon's first version, andHexToBytesJon2
is the second variant.HexToBytesJonCiC
is Jon's version with CodesInChaos's suggested code.HexToBytesJase
is my attempt, based on both the above but with alternative nybble conversion which eschews error checking, and branching:public static byte[] HexToBytesJase(string hexString) { if ((hexString.Length & 1) != 0) { throw new ArgumentException("Input must have even number of characters"); } byte[] ret = new byte[hexString.Length/2]; for (int i = 0; i < ret.Length; i++) { int high = hexString[i*2]; int low = hexString[i*2+1]; high = (high & 0xf) + ((high & 0x40) >> 6) * 9; low = (low & 0xf) + ((low & 0x40) >> 6) * 9; ret[i] = (byte)((high << 4) | low); } return ret; }
这篇关于从十六进制大部分重量轻转换在C#字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!