Reputation: 57
I have a byte array containing value like this:
byte[] data={0x04,0x00};
I need to convert it to a string a print it as str_data=0x400
But when i convert this to string the data is printed as 40 where last 0x00 is considered as only 0.
I am new to C# and I am struggling to solve this. Please help.
Upvotes: 0
Views: 1576
Reputation: 1219
Your question is a bit unclear, but I think what you want is the X2
format specifier for bytes, which will print your bytes as two hex digits, e.g.:
byte b = 0x40;
Console.WriteLine( b.ToString( "X2" ) ); // Prints '40'
Convert each of your bytes into a string (with e.g. LINQ's Select
method), then join them and add the "0x" prefix.
Upvotes: 1