0

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)?

Eamorr
  • 225
  • I should say that I could ask my users to enter "0x1e240" instead of "123456". But as most of my users do not understand hex (nor do I, it seems...), I need to do it this way... – Eamorr Mar 19 '16 at 22:09
  • Why ask this here rather than on the support forum for Solidity? – Rob Arthan Mar 19 '16 at 22:54
  • Fair comment, but I just thought first principles maths brains would be much quicker at this conversion gymnastics. – Eamorr Mar 19 '16 at 23:18
  • But the maths brains here have no idea what you can and can't do in Solidity. – Rob Arthan Mar 19 '16 at 23:25
  • Yeah. It's tricky in Solidity, because there are no decimals. Only unsigned ints... And uints are 256 bits long by default! – Eamorr Mar 20 '16 at 01:05

1 Answers1

0

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
Eamorr
  • 225