Radix conversion issue

34 Views Asked by At

I have a funny radix conversion problem. I'm programming in a language called Solidity. It's very primitive and doesn't have many of the standard string and math operators that you'd expect in other languages.

I am passing the following data to my program:

"123456"

This compound string presents inside my program as three bytes (native byte types):

"12", "34" and "56"

I have successfully converted these strings to:

18, 52 and 86 (stored natively as uint types)

My question is this: how the hell do I convert 18, 52 and 86 to 123456 (i.e. a uint with the value one-hundred-and-twenty-three-thousand-four-hundred-and-fifty-six)?

1

There are 1 best solutions below

1
On

Here is the answer:

//msg.data="0x1234"

uint i=0;
uint dec=0;
for (i = 0; i < msg.data.length; i++) {
        byte e=msg.data[msg.data.length-i-1];
        uint f=uint(e);
        f=((f/16)*10)+(f%16);
        dec+=(f*(100**i));
}

//dec now contains the `uint` 1234