Here's a short but useful static funtion: converting a uint into a properly formatted hexadecimal string value in C#.
public static string ConvertToHexString(uint value) { StringBuilder builder = new StringBuilder("0x"); builder.Append(Convert.ToString(value, 16).PadLeft(8, '0')); return builder.ToString(); }
This is pretty self-explanator, but the ToString method on Convert takes an optional second parameter for the base, which we sit to base 16 for the hexadecimal string value. If you run this test code:
string test = ConvertToHexString(177);
The hex string value for 177 returned is: 0x000000b1. The PadLeft method after the ToString call gives us the leading zeroes. Without PadLeft, all we would see is 0xb1.



Leave a comment