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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With