Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format variables in C# or VB.NET? [duplicate]

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?

like image 209
fifamaniac04 Avatar asked Nov 24 '25 16:11

fifamaniac04


2 Answers

int x = 255;
Console.WriteLine("x is: {0} and in HEX, x is: {0:X}", x);
like image 65
SliverNinja - MSFT Avatar answered Nov 27 '25 07:11

SliverNinja - MSFT


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");
like image 39
Olivier Jacot-Descombes Avatar answered Nov 27 '25 07:11

Olivier Jacot-Descombes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!