How do i convert a byte[] to a string, every time i attempt it i get System.Byte[] instead of the value.
Also How do i get the value in Hex instead of a decimal?
How do i convert a byte[] to a string, every time i attempt it i get System.Byte[] instead of the value.
Also How do i get the value in Hex instead of a decimal?
You have to know the encoding of the string represented in bytes, but you can say System.Text.UTF8Encoding.GetString(bytes)
or System.Text.ASCIIEncoding.GetString(bytes)
. (I'm doing this from memory, so the API may not be exactly correct, but it's very close.)
For the answer to your second question, see this question.
Well I don't convert bytes to hex often so I have to say I don't know if there is a better way then this, but here is a way to do it.
StringBuilder sb = new StringBuilder();
foreach (byte b in myByteArray)
sb.Append(b.ToString("X2"));
string hexString = sb.ToString();
As others have said it depends on the encoding of the values in the byte array. Despite this you need to be very careful with this sort of thing or you may try to convert bytes that are not handled by the chosen encoding.
Jon Skeet has a good article about encoding and unicode in .NET. Recommended reading.
Hex, Linq-fu:
string.Join("",ba.Select(b => b.ToString("X2")).ToArray())
There is a built in method for this:
byte[] data = { 1, 2, 4, 8, 16, 32 };
string hex = BitConverter.ToString(data);
Result: 01-02-04-08-10-20
If you want it without the dashes, just remove them:
string hex = BitConverter.ToString(data).Replace("-", string.Empty);
Result: 010204081020
If you want a more compact representation, you can use Base64:
string base64 = Convert.ToBase64String(data);
Result: AQIECBAg
You combine LINQ with string methods:
string hex = string.Join("",
bin.Select(
bin => bin.ToString("X2")
).ToArray());
I like using extension methods for conversions like this, even if they just wrap standard library methods. In the case of hexadecimal conversions, I use the following hand-tuned (i.e., fast) algorithms:
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;
}
Nice way to do this with LINQ...
var data = new byte[] { 1, 2, 4, 8, 16, 32 };
var hexString = data.Aggregate(new StringBuilder(),
(sb,v)=>sb.Append("X2")
).ToString();