Binary to hex Conversion ?
-
Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.
-
Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.
-
Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.
The convert class can also be used for this:
Convert.ToString(number, base)
where base can be 2 (binary), 10 (decimal) or 16 (hexadecimal).
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
The convert class can also be used for this:
Convert.ToString(number, base)
where base can be 2 (binary), 10 (decimal) or 16 (hexadecimal).
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thanks for your reply. If you just convert it as you have shown it Eliminates the "0". Say for example you have to convert 0000111111111000 into Hex.So it will show FF8 instead 0FF8 Well I was able to over come that by doing following:
string MyBinary = "0000111111111000";
string MyHex = String.Format("{0:X4}", Convert.ToInt32(MyBinary, 2)); -
you can do it on this way
byte b = 15;
Console.WriteLine(b.ToString("X"));Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
Thanks .....!!!!!!!
-
Thanks for your reply. If you just convert it as you have shown it Eliminates the "0". Say for example you have to convert 0000111111111000 into Hex.So it will show FF8 instead 0FF8 Well I was able to over come that by doing following:
string MyBinary = "0000111111111000";
string MyHex = String.Format("{0:X4}", Convert.ToInt32(MyBinary, 2));When I need padding I use the string.PadLeft method. A couple of functions like this are all that's needed.
public static string ToHexString(int value)
{
return ToHexString(value, 0);
}
public static string ToHexString(int value, int bytes)
{
string result = Convert.ToString(value, 16);
if (bytes > 0)
result = result.PadLeft(bytes * 2, '0');
return result;
}Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.
int b = 15;
string s=b.ToString("X8");I advice to always specify the width you want; padding zeroes will be prefixed when necessary. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.