Butzke
Butzke

Reputation: 561

Format int to hex string

I have some int values that I want to convert to a string but in hex. This hex value should be formatted always by 2 digits.

Example below:

int a = 10;
int b = 20;

//returns the value in hex
string c = a.toString("x"); // a
string d = b.toString("x"); // 14

What I want is that always that the hex value results in two digits. Shows like "0a", not only "a".

I'm using convert a int to a formatted string,

int e = 1;
string f = e.toString("D2"); // 01

Have a way to the two things together? To convert the int to a hex formatted string?

Upvotes: 0

Views: 11336

Answers (2)

Koushik
Koushik

Reputation: 372

you can use this

int e = 1;
string f = e.toString("x2");  

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1500675

Have a way to the two things togheter?

Yes - you just use x2. You already have the hex bit with x and the "2 characters" part with D2 - you just need to combine them.

See the documentation for standard numeric format strings for more information.

Upvotes: 5

Related Questions