如何将字节[]转换为字符串?每次我尝试,我都
系统。Byte []
而不是数值。
另外,我如何得到十六进制而不是小数的值?
如何将字节[]转换为字符串?每次我尝试,我都
系统。Byte []
而不是数值。
另外,我如何得到十六进制而不是小数的值?
当前回答
非常快速的扩展方法(带反转):
public static class ExtensionMethods {
public static string ToHex(this byte[] data) {
return ToHex(data, "");
}
public static string ToHex(this byte[] data, string prefix) {
char[] lookup = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int i = 0, p = prefix.Length, l = data.Length;
char[] c = new char[l * 2 + p];
byte d;
for(; i < p; ++i) c[i] = prefix[i];
i = -1;
--l;
--p;
while(i < l) {
d = data[++i];
c[++p] = lookup[d >> 4];
c[++p] = lookup[d & 0xF];
}
return new string(c, 0, c.Length);
}
public static byte[] FromHex(this string str) {
return FromHex(str, 0, 0, 0);
}
public static byte[] FromHex(this string str, int offset, int step) {
return FromHex(str, offset, step, 0);
}
public static byte[] FromHex(this string str, int offset, int step, int tail) {
byte[] b = new byte[(str.Length - offset - tail + step) / (2 + step)];
byte c1, c2;
int l = str.Length - tail;
int s = step + 1;
for(int y = 0, x = offset; x < l; ++y, x += s) {
c1 = (byte)str[x];
if(c1 > 0x60) c1 -= 0x57;
else if(c1 > 0x40) c1 -= 0x37;
else c1 -= 0x30;
c2 = (byte)str[++x];
if(c2 > 0x60) c2 -= 0x57;
else if(c2 > 0x40) c2 -= 0x37;
else c2 -= 0x30;
b[y] = (byte)((c1 << 4) + c2);
}
return b;
}
}
在上面的速度测试中击败所有其他人:
=== Long string test BitConvertReplace calculation Time Elapsed 2415 ms StringBuilder calculation Time Elapsed 5668 ms LinqConcat calculation Time Elapsed 11826 ms LinqJoin calculation Time Elapsed 9323 ms LinqAgg calculation Time Elapsed 7444 ms ToHexTable calculation Time Elapsed 1028 ms ToHexAcidzombie calculation Time Elapsed 1035 ms ToHexPatrick calculation Time Elapsed 814 ms ToHexKurt calculation Time Elapsed 1604 ms ByteArrayToHexString calculation Time Elapsed 1330 ms === Many string test BitConvertReplace calculation Time Elapsed 2238 ms StringBuilder calculation Time Elapsed 5393 ms LinqConcat calculation Time Elapsed 9043 ms LinqJoin calculation Time Elapsed 9131 ms LinqAgg calculation Time Elapsed 7324 ms ToHexTable calculation Time Elapsed 968 ms ToHexAcidzombie calculation Time Elapsed 969 ms ToHexPatrick calculation Time Elapsed 956 ms ToHexKurt calculation Time Elapsed 1547 ms ByteArrayToHexString calculation Time Elapsed 1277 ms
其他回答
private static string GuidToRaw(Guid guid)
{
byte[] bytes = guid.ToByteArray();
int сharCount = bytes.Length * 2;
char[] chars = new char[сharCount];
int index = 0;
for (int i = 0; i < сharCount; i += 2)
{
byte b = bytes[index++];
chars[i] = GetHexValue((int)(b / 16));
chars[i + 1] = GetHexValue((int)(b % 16));
}
return new string(chars, 0, chars.Length);
}
private static char GetHexValue(int i)
{
return (char)(i < 10 ? i + 48 : i + 55);
}
我喜欢将扩展方法用于这样的转换,即使它们只是包装标准库方法。在十六进制转换的情况下,我使用以下手动调优(即快速)算法:
public static string ToHex(this byte[] bytes)
{
char[] c = new char[bytes.Length * 2];
byte b;
for(int bx = 0, cx = 0; bx < bytes.Length; ++bx, ++cx)
{
b = ((byte)(bytes[bx] >> 4));
c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);
b = ((byte)(bytes[bx] & 0x0F));
c[++cx]=(char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);
}
return new string(c);
}
public static byte[] HexToBytes(this string str)
{
if (str.Length == 0 || str.Length % 2 != 0)
return new byte[0];
byte[] buffer = new byte[str.Length / 2];
char c;
for (int bx = 0, sx = 0; bx < buffer.Length; ++bx, ++sx)
{
// Convert first half of byte
c = str[sx];
buffer[bx] = (byte)((c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')) << 4);
// Convert second half of byte
c = str[++sx];
buffer[bx] |= (byte)(c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0'));
}
return buffer;
}
用LINQ做这件事的好方法…
var data = new byte[] { 1, 2, 4, 8, 16, 32 };
var hexString = data.Aggregate(new StringBuilder(),
(sb,v)=>sb.Append(v.ToString("X2"))
).ToString();
非常快速的扩展方法(带反转):
public static class ExtensionMethods {
public static string ToHex(this byte[] data) {
return ToHex(data, "");
}
public static string ToHex(this byte[] data, string prefix) {
char[] lookup = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int i = 0, p = prefix.Length, l = data.Length;
char[] c = new char[l * 2 + p];
byte d;
for(; i < p; ++i) c[i] = prefix[i];
i = -1;
--l;
--p;
while(i < l) {
d = data[++i];
c[++p] = lookup[d >> 4];
c[++p] = lookup[d & 0xF];
}
return new string(c, 0, c.Length);
}
public static byte[] FromHex(this string str) {
return FromHex(str, 0, 0, 0);
}
public static byte[] FromHex(this string str, int offset, int step) {
return FromHex(str, offset, step, 0);
}
public static byte[] FromHex(this string str, int offset, int step, int tail) {
byte[] b = new byte[(str.Length - offset - tail + step) / (2 + step)];
byte c1, c2;
int l = str.Length - tail;
int s = step + 1;
for(int y = 0, x = offset; x < l; ++y, x += s) {
c1 = (byte)str[x];
if(c1 > 0x60) c1 -= 0x57;
else if(c1 > 0x40) c1 -= 0x37;
else c1 -= 0x30;
c2 = (byte)str[++x];
if(c2 > 0x60) c2 -= 0x57;
else if(c2 > 0x40) c2 -= 0x37;
else c2 -= 0x30;
b[y] = (byte)((c1 << 4) + c2);
}
return b;
}
}
在上面的速度测试中击败所有其他人:
=== Long string test BitConvertReplace calculation Time Elapsed 2415 ms StringBuilder calculation Time Elapsed 5668 ms LinqConcat calculation Time Elapsed 11826 ms LinqJoin calculation Time Elapsed 9323 ms LinqAgg calculation Time Elapsed 7444 ms ToHexTable calculation Time Elapsed 1028 ms ToHexAcidzombie calculation Time Elapsed 1035 ms ToHexPatrick calculation Time Elapsed 814 ms ToHexKurt calculation Time Elapsed 1604 ms ByteArrayToHexString calculation Time Elapsed 1330 ms === Many string test BitConvertReplace calculation Time Elapsed 2238 ms StringBuilder calculation Time Elapsed 5393 ms LinqConcat calculation Time Elapsed 9043 ms LinqJoin calculation Time Elapsed 9131 ms LinqAgg calculation Time Elapsed 7324 ms ToHexTable calculation Time Elapsed 968 ms ToHexAcidzombie calculation Time Elapsed 969 ms ToHexPatrick calculation Time Elapsed 956 ms ToHexKurt calculation Time Elapsed 1547 ms ByteArrayToHexString calculation Time Elapsed 1277 ms
十六进制,Linq-fu:
string.Concat(ba.Select(b => b.ToString("X2")).ToArray())
与时俱进
正如@RubenBartelink所指出的,没有将IEnumerable<string>转换为数组的代码:ba。Select(b => b. tostring ("X2"))在4.0之前不工作,相同的代码现在在4.0上工作。
这段代码…
byte[] ba = { 1, 2, 4, 8, 16, 32 };
string s = string.Concat(ba.Select(b => b.ToString("X2")));
string t = string.Concat(ba.Select(b => b.ToString("X2")).ToArray());
Console.WriteLine (s);
Console.WriteLine (t);
...在。net 4.0之前,输出是:
System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[System.Byte,System.String]
010204081020
在。net 4.0以后,字符串。Concat有一个接受IEnumerable的重载。因此,在4.0版本中,上述代码对于变量s和t将有相同的输出
010204081020
010204081020
4.0之前,ba。Select(b => b.ToString("X2"))将重载(对象arg0), IEnumerable<string>进入适当重载的方式,即(params string[] values),是我们需要将IEnumerable<string>转换为字符串数组。4.0之前,字符串。Concat有10个过载功能,在4.0现在是12个