Printing hex digits

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Printing hex digits

meino.cramer
Hi,

I have to convert *a lot* of decimal numbers, given textually in a
ASCII file into hexadecimal.

The conversion itself is not the problem ("io.write(string.format(
"%X",....))").

But the hexadecimal number have to have leading zeroes...

The format "%X" seems not to support leading zeroes as in C....

How can I accomplish this with not too much overhead with lua 5.1x ?

Thank you very much in advance for any help!
Best regards,
mcc



Reply | Threaded
Open this post in threaded view
|

Re: Printing hex digits

Kaj Eijlers
On my system "%04x" works perfectly fine. You'll have to give it a length or it won't need to prefix with zeroes.

Kaj


On Sun, Jun 2, 2013 at 2:31 AM, <[hidden email]> wrote:
Hi,

I have to convert *a lot* of decimal numbers, given textually in a
ASCII file into hexadecimal.

The conversion itself is not the problem ("io.write(string.format(
"%X",....))").

But the hexadecimal number have to have leading zeroes...

The format "%X" seems not to support leading zeroes as in C....

How can I accomplish this with not too much overhead with lua 5.1x ?

Thank you very much in advance for any help!
Best regards,
mcc




Reply | Threaded
Open this post in threaded view
|

Re: Printing hex digits

Eike Decker
In reply to this post by meino.cramer


Am 02.06.2013 08:35 schrieb <[hidden email]>:
>
> Hi,
>
> I have to convert *a lot* of decimal numbers, given textually in a
> ASCII file into hexadecimal.
>
> The conversion itself is not the problem ("io.write(string.format(
> "%X",....))").
>
> But the hexadecimal number have to have leading zeroes...
>
> The format "%X" seems not to support leading zeroes as in C....
>
> How can I accomplish this with not too much overhead with lua 5.1x ?
>
> Thank you very much in advance for any help!
> Best regards,
> mcc
>
>
>

string.format("0x%x", 245)