[英]C# - Fast Method to Convert Byte Array to Hex String
I want to convert a Byte array as fast as possible to a Hex String.我想尽快将字节数组转换为十六进制字符串。
So through my previous question, I found the following code :所以通过我之前的问题,我找到了以下代码:
private static readonly uint[] _lookup32 = CreateLookup32();
private static uint[] CreateLookup32()
{
var result = new uint[256];
for (int i = 0; i < 256; i++)
{
string s = i.ToString("X2");
result[i] = ((uint)s[0]) + ((uint)s[1] << 16);
}
return result;
}
private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
var lookup32 = _lookup32;
var result = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
var val = lookup32[bytes[i]];
result[2 * i] = (char)val;
result[2 * i + 1] = (char)(val >> 16);
}
return new string(result);
}
This works great but the Issue with it is that the output string looks like this:这很好用,但问题在于输出字符串如下所示:
output: 0F42000AAD24120024
but i need it like this: 0F 42 00 0A AD 24 12 00 24
As my coding knowledge is kinda meh on "cryptic" looking algorithms I don't know where and how to add code so it would add a blank space between each 2 Bytes - (Hexoutputstring + " ") to it.由于我的编码知识对“神秘”的算法有点熟悉,我不知道在哪里以及如何添加代码,因此它会在每个 2 个字节之间添加一个空格 - (Hexoutputstring +“”)。
I could loop trough the string and add every 2 charackters a blank space but that would hugely increase the amount of time it needs to give me a useful results as appending strings is slow.我可以循环遍历字符串并每 2 个字符添加一个空格,但这会大大增加它需要的时间来提供有用的结果,因为附加字符串很慢。
Could someone help me with the code above?有人可以帮我上面的代码吗? Thanks you :)
谢谢 :)
private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
var lookup32 = _lookup32;
var byteCount = bytes.Length;
var result = new char[3* byteCount - 1];
for (int i = 0; i < byteCount; i++)
{
var val = lookup32[bytes[i]];
int index = 2 * i;
result[index] = (char)val;
result[index + 1] = (char)(val >> 16);
if (i < byteCount - 1) result[index + 2] = ' ';
}
return new string(result);
}
If performance is one of your main concerns, I would approach it something like this:如果性能是您的主要关注点之一,我会采用以下方法:
private static readonly char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
char[] buffer = new char[bytes.Length * 3];
int index = 0;
for (int i = 0; i < bytes.Length; i++)
{
if (index > 0)
buffer[index++] = ' ';
buffer[index++] = digits[(bytes[i] >> 4) & 0xf];
buffer[index++] = digits[bytes[i] & 0xf];
}
return new string(buffer, 0, index);
}
The following version doesn't require any lookup array, but I'm not sure if it's as fast.以下版本不需要任何查找数组,但我不确定它是否一样快。
private static string ByteArrayToHexViaLookup32(byte[] bytes)
{
char[] buffer = new char[bytes.Length * 3];
int index = 0;
for (int i = 0; i < bytes.Length; i++)
{
if (index > 0)
buffer[index++] = ' ';
buffer[index++] = GetDigit((bytes[i] >> 4) & 0xf);
buffer[index++] = GetDigit(bytes[i] & 0xf);
}
return new string(buffer, 0, index);
}
private char GetDigit(int value)
{
if (value < 10)
return (char)('0' + value);
return (char)('7' + value);
}
Both versions insert a space between bytes.两个版本都在字节之间插入一个空格。
private static string ByteArrayToStringHex(byte[] bytes)
{
string hexValue = BitConverter.ToString(bytes);
hexValue = hexValue.Replace("-", " ");
return hexValue;
}
I think it results the same values as which you want我认为它的结果与您想要的值相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.