Pregunta

How can I take as a string the significant part of a hex/ulong number?

For example, if I have 0x00000321321, I want to get "321321".

If I try something like:

ulong x = 0x0000023632763;
string s = x.toString();

I get 593700707, so it is not what i want.

Is that possible ?

Thank you!

¿Fue útil?

Solución

Use the ToString(string format) override with a standard hexadecimal formatting "x" , along with TrimStart() to get rid of leading zeros:

string s = x.ToString("x");

(Thanks to Keith N. for pointing out the Trim is not necessary.)

Otros consejos

How about x.toString("x")? I don't have a C# handy to try it out but that should output the number in hex. See the docs.

very easy... to string has lots of options on how to create strings...

x.ToString("X");

it will only convert the significant part. Only choice is x or X controls whether you get capital letters or not for hex digits.

ToString suports the standard nnumeric format. Here is the e MSDN link

int value; 

value = 0x2045e;
Console.WriteLine(value.ToString("x"));
// Displays 2045e
Console.WriteLine(value.ToString("X"));
// Displays 2045E
Console.WriteLine(value.ToString("X8"));
// Displays 0002045E 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top