3

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?

Community
  • 1
  • 1
fifamaniac04
  • 2,343
  • 12
  • 49
  • 72

7 Answers7

2
int x = 255;
Console.WriteLine("x is: {0} and in HEX, x is: {0:X}", x);
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
2

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");
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

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));
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • How does this get printed to the screen? The OP wants the `printf` equivalent? – SliverNinja - MSFT Jun 21 '12 at 15:27
  • The question is how to format a string. In C/C++, this was commonly done with a print directly. In C#/VB that need not be the case. – Servy Jun 21 '12 at 15:30
  • 1
    Incorrect - [`sprintf`](http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/) would be the equivalent of [`String.Format`](http://msdn.microsoft.com/en-us/library/system.string.format.aspx), whereas [`printf`](http://www.cplusplus.com/reference/clibrary/cstdio/printf/) would be the equivalent of [`Console.WriteLine`](http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx) – SliverNinja - MSFT Jun 21 '12 at 15:34
  • I didn't say it *couldn't* be done in C, I just said it's less commonly done. – Servy Jun 21 '12 at 15:40
1
 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

Patrizio Bertoni
  • 2,582
  • 31
  • 43
Darren
  • 68,902
  • 24
  • 138
  • 144
  • 1
    [`Console.WriteLine` already takes a `StringFormat`](http://msdn.microsoft.com/en-us/library/ttxecb1c.aspx) – SliverNinja - MSFT Jun 21 '12 at 15:26
  • @SliverNinja true, but it can be useful for this particular case to demonstrate that it's using `string.Format` internally. It will point him to the correct page(s) in the documentation more easily. – Servy Jun 21 '12 at 15:29
  • I personally would use String.Format but always useful to know. Thanks – Darren Jun 21 '12 at 15:31
0

The printf equivalent is String.Format:

String.Format("{0:x}", 0xBEEF);

or just use the int.ToString method:

int MyInt = 0xBEEF;
MyInt.ToString("x");
Matt
  • 7,100
  • 3
  • 28
  • 58
0

You use String.Format

string.Format("x is: {0} and in HEX, x is: {1:X}", x, x);

dtsg
  • 4,408
  • 4
  • 30
  • 40
0

Use ToString("X")

Console.WriteLine("{0} hex equivalent =  {1}", 456, 456.ToString("X"));

Covert to hex, and convert back

Tilak
  • 30,108
  • 19
  • 83
  • 131