lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Norbert Kiesel wrote:
I'm trying to convert arbitrary long decimals (encoded as a string of
digits) to it's hex representation (i.e. a string of hexadecimal
digits).  For short decimals, format('%x') is the way to go.  That
looses out however for e.g. '5766297887677882160' because we loose
precision when it's internally converted to a (floating point) number:
it returns '50060160b0202000' instead of '50060160b0201f30'.

So please help me out.  Here is what I came up with, is that correct or
is there a better/faster way to do it (I know that BigNum can do it, but
this is not pure Lua).

At first glance, I don't believe calculating partial results from left to right in this way works... have you actually tried the code?

function tohex(decimal)
[snip]
      for i = 1, #decimal do
[snip]

Tons of FLOSS arbitrary precision code around, I think they are worth a look.

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia