C #: string format

I came across a code. Can anyone shed light on him. Plz be kind if someone finds this a bit basic.

string str= String.Format("{0,2:X2}", (int)value);

Thank you for your time.

+5
source share
3 answers
Format

Xreturns the hexadecimal representation of yours value.

for example String.Format("{0:X}", 10)will return "A", not"10"

X2will add zeros to the left if your hexadecimal representation is less than two characters

for example String.Format("{0:X2}", 10)will return "0A", not"A"

0,2will add spaces to the left if the resulting number of characters is less than 2.

for example String.Format("{0,3:X2}", 10)will return " 0A", but not"0A"

, {0,2:X2} , , , , . , , ,2 , {0:X2} .

:

: , . ,

String.Format("{0:X} {1:N} {0:N}", 10, 20)

, 10 ( 0) , 20 ( 1) , 10 ( 0) .

0,2 0 ,2, , . ,

String.Format("{0,1} {1,2} {0,4}", 10, 20)

, , , , , , . - .

+12
{0,2:X2}

  • 0,2 - 10 10
  • X2 - 10 hexadecimel 0A.

Update

String.Format("{0,2:X2}", (int)value); // where value = 10

: 0A

Live : http://ideone.com/NW0U26


"{0,2:X2}" "{0:X2}", .

: MSDN

+6

MSDN :

{index[,alignment][:formatString]}

( ) :

0 - .
,2 - , , .
:X2 - formatString. , ( ) 2. 2 , .

, X2 2.

For more information on the format string, see here:
Composite Formatting
Standard Number Format Strings

+2
source

All Articles