Convert int32 to string in base 16

Infiniti Fizz picture Infiniti Fizz · Jun 18, 2012 · Viewed 8.1k times · Source

I'm currently trying to convert a .NET JSON Encoder to NETMF but have hit a problem with Convert.ToString() as there isn't such thing in NETMF.

The original line of the encoder looks like this:

Convert.ToString(codepoint, 16);

And after looking at the documentation for Convert.ToString(Int32, Int32) it says it's for converting an int32 into int 2, 8, 10 or 16 by providing the int as the first parameter and the base as the second.

What are some low level code of how to do this or how would I go about doing this?

As you can see from the code, I only need conversion from an Int32 to Int16.

EDIT

Ah, the encoder also then wants to do:

PadLeft(4, '0');

on the string, is this just adding 4 '0' + '0' + '0' + '0' to the start of the string?

Answer

cdkMoose picture cdkMoose · Jun 18, 2012

If you mean you want to change a 32-bit integer value into a string which shows the value in hexadecimal:

string hex = intValue.ToString("x");

For variations, please see Stack Overflow question Convert a number into the hex value in .NET.

Disclaimer: I'm not sure if this function exists in NETMF, but it is so fundamental that I think it should.