REQ: How To Do Hex Formatting...

D

David Elliott

I am trying to format bytes and characters in a hexadecimal format that is preceeded with Zeros.

I found some information in the help section "Custom Numeric Format Strings" and
"Standard Numeric Format Strings Output Examples" but am unable to get it to work properly.

I can print by using the following code
byte data;

for (int i = 0; i < 30; i++)
{
data = (byte)i;
System.Console.Out.Write("{0:x} ", data);
}

But this only displays
0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d

I would like
01 02 03 04 05 06 07 08

and eventually use it for characters to display
0001 0002 0003 0004 0005 ...

Any help would be appreciated.

Cheers,
Dave

(e-mail address removed)
 
B

Bill Styles

David Elliott said:
I am trying to format bytes and characters in a hexadecimal format
that is preceeded with Zeros.

how about

System.Console.Out.Write(i.ToString("x").PadLeft(5,'0') + " ");
 
J

Jon Skeet [C# MVP]

David Elliott said:
I am trying to format bytes and characters in a hexadecimal format
that is preceeded with Zeros.

I think you're after:

Console.Write ("{0:x2} ", data);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top