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"
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.