简体   繁体   中英

What does `{0:X2}` mean in this code sample?

In the below code sample, what does {0:X2} mean? This is from the reflection section of the MCTS Application Development Foundation book (covering dynamic code, etc.).

foreach(Byte b in body.GetILAsBodyArray())
{
Console.Write("{0:X2}", b);
}

This uses the same format as String.Format(). Check out the following reference:

http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

  • X = Hexadecimal format
  • 2 = 2 characters

Beware the length specified is not respected if the number is too large to fit the length.

 long a = 123456789;
 Console.Write("{0:X2}", a);
 ->   75BCD15

This is especially important if you want to show negative hex numbers where all the high bits are set to 1's.

 long a = -1;
 Console.Write("{0:X2}", a);
 ->  FFFFFFFFFFFFFFFF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM