Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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); } 
like image 414
GurdeepS Avatar asked Jan 01 '09 18:01

GurdeepS


2 Answers

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
like image 169
BenAlabaster Avatar answered Sep 30 '22 22:09

BenAlabaster


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 
like image 32
Niels Gjeding Olsen Avatar answered Sep 30 '22 21:09

Niels Gjeding Olsen