Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?
In C, you can do something like
int x = 255;
printf("x is: %d and in HEX, x is: %x", x, x);
How can I do that in C# or VB.NET? Print the variable's hex equivalent?
Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?
In C, you can do something like
int x = 255;
printf("x is: %d and in HEX, x is: %x", x, x);
How can I do that in C# or VB.NET? Print the variable's hex equivalent?
int x = 255;
Console.WriteLine("x is: {0} and in HEX, x is: {0:X}", x);
Like this
Console.WriteLine("x is: {0} and in HEX, x is: {0:X}", x);
If you need only the string
string formatted = String.Format("x is: {0} and in HEX, x is: {0:X}", x);
This is called Composite Formatting. {n} acts as placeholder for the parameters that follow, where n is the zero-based number of the parameter. You can specify an optional format after : in the placeholder.
You can convert an int to string by specifying a format
string hex = x.ToString("X");
You can use String.Format("{0:X}", number) to format as hex.
Console.Write(String.Format("x is: {0} and in HEX, x is : {0:X}", x));
int x = 500;
Console.WriteLine("x is: {0} and in HEX, x is: {1:X}", x, x);
Would output
x is: 500 and in HEX, x is: 1F4
Source: http://msdn.microsoft.com/en-us/library/bb311038.aspx
The printf equivalent is String.Format:
String.Format("{0:x}", 0xBEEF);
or just use the int.ToString method:
int MyInt = 0xBEEF;
MyInt.ToString("x");
You use String.Format
string.Format("x is: {0} and in HEX, x is: {1:X}", x, x);
Use ToString("X")
Console.WriteLine("{0} hex equivalent = {1}", 456, 456.ToString("X"));