Hex String Formatting

D

David Hall

byte b = 10;

Console.WriteLine("{0} = 0x{0,2:X}", b);

// This example displays "10 = 0x A". Note the space between 0x and A

// I want it to display as: "10 = 0x0A" Note the zero between 0x and A

// How can I change the format string

// to pad it to 2 digits using a leading 0 if necessary

// instead of a space in the hex representation?

// Dave
 
R

Ray Hsieh (Ray Djajadinata)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi David,

Use X2 instead of just X.

David Hall wrote:
| byte b = 10;
|
| Console.WriteLine("{0} = 0x{0,2:X}", b);
|
| // This example displays "10 = 0x A". Note the space between 0x and A
|
| // I want it to display as: "10 = 0x0A" Note the zero between 0x and A
|
| // How can I change the format string
|
| // to pad it to 2 digits using a leading 0 if necessary
|
| // instead of a space in the hex representation?
|
| // Dave
|
|


- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nyu0wEwccQ4rWPgRAigrAJ0TcDPfLDYBHHY9+3xbLolJUX5aZQCdFlMv
DC5EfUzPvbvQc8zlN8Ym3S0=
=cokt
-----END PGP SIGNATURE-----
 
D

David Hall

Thanks, The following changes both work correctly, and messing with the
numbers gives me a little more insight as to how the format specifers work
in practice...

byte b = 10;

Console.WriteLine("{0} = 0x{0,0:X2}", b);

Console.WriteLine("{0} = 0x{0,2:X2}", b);

Just for my own curiosity, can anyone show me where to find this info on
MSDN? I'm glad it works, but I still can't find any docs that say it
should...

Thanks again,

Dave
 
R

Ray Hsieh (Ray Djajadinata)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi David,

I don't have the exact page with me right now, but searching for "format
string composite" should give you what you want.

David Hall wrote:

| Thanks, The following changes both work correctly, and messing with the
| numbers gives me a little more insight as to how the format specifers work
| in practice...
|
| byte b = 10;
|
| Console.WriteLine("{0} = 0x{0,0:X2}", b);
|
| Console.WriteLine("{0} = 0x{0,2:X2}", b);
|
| Just for my own curiosity, can anyone show me where to find this info on
| MSDN? I'm glad it works, but I still can't find any docs that say it
| should...
|
| Thanks again,
|
| Dave

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nz/GwEwccQ4rWPgRAphcAJ4xYjn0nErsNfZZcnLIA0MG1kVpjACeJ7jb
2SBKgmaQmTZFL45K/zarQuM=
=WO/6
-----END PGP SIGNATURE-----
 

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