tags:

views:

104

answers:

2

In VB what is the difference between

String.Format("{0:X1}", abyte)

and

String.Format("{0:X2}", abyte)

abyte is of type byte

+5  A: 

See MSDN:

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

Also, this format is only supported for numeric types, so abyte is interpreted as such.

If abyte represents a number greater than F (15 dec), X and X2 are equivalent

String.Format("{0:X}",16)  => "10"
String.Format("{0:X2}",16) => "10"
String.Format("{0:X3}",16) => "010"
String.Format("{0:X4}",16) => "0010"

and so on

Vinko Vrsalovic
+3  A: 

The value after the X specifies the minimum number of characters in the formatted number.

String.Format("{0:X1}", 12) => "C"    
String.Format("{0:X2}", 12) => "0C"
String.Format("{0:X3}", 12) => "00C"
String.Format("{0:X4}", 12) => "000C"
String.Format("{0:X5}", 12) => "0000C"
String.Format("{0:X6}", 12) => "00000C"
String.Format("{0:X7}", 12) => "000000C"
String.Format("{0:X8}", 12) => "0000000C"

And FYI, the maximum value after X is 99.

Gavin Miller
what will be the result of String.Format("{0:X1}", 63)
@unknown - It would take you all of 5 minutes to test it! But LFSR's answer should answer your question.
Chris Dunaway