C # How to format a number in hexadecimal format with the prefix '0x'

How to format the number in hexadecimal format with the prefix "0x"?

For example:

int space = 32; MessageBox.Show(space.ToString("'0x'X4")); // Output 0xX4 instead of 0x0020 

I clicked on this link: Custom Number Format Strings http://msdn.microsoft.com/en-us/library/0c899ak8.aspx Literal Line Separator: Indicates that private characters should be copied to the result string without changes. But this does not work for "X4" (it works for "#"), something is strange.

I use it in DataGridView.DefaultCellStyle.Format, so I can not use:

 "0x{0:X4}", space 

Thanks. Peter

+6
c # format hex
source share
3 answers

try checking here: Creating a custom format string in dataGridView

0
source share
 int space = 32; MessageBox.Show("0x"+space.ToString("X")); 

If you want to print 0x0020 :

 MessageBox.Show("0x"+space.ToString("X4")); 
+5
source share
 string.Format("0x{0:x8}", ii); 
+1
source share

All Articles