1

Possible Duplicate:
How to convert an integer to fixed length hex string in C#?

I need to format a range of UInt32 values to a fixed length hexadecimal representation. Formatting in hex is the easy part, but the resulting strings are between 6 and 8 characters wide. How do I format the string to always be 8 characters wide?

Here is an example of what I am doing currently:

string valueA = Convert.ToString(UInt32.MaxValue, 16); // result is "ffffffff"
string valueB = Convert.ToString(UInt32.MinValue, 16); // result is "0", i want "00000000"
Community
  • 1
  • 1
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

5

How to: Pad a Number with Leading Zeros:

Console.WriteLine("{0:D8} {0:X8}", intValue);
// or
Console.WriteLine("{0} {1}", intValue.ToString("D8"), intValue.ToString("X8"));

Yields 01023983 and 000F9FEF.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272