Jimmy O'Regan wrote:
> 2008/8/20 Phil <philb at vodafone.ie>:
>> Hi,
>>>> I thought this one was obvious but seemingly not. I have a value, in excess
>> of 2^32 that I want to print out using perl (ancient RedHat 8.0 on 32 bit).
>> The output should be padded to 10 characters.
>>>> First test works fine:
>> my $foo = 3188931580; # > 2147483648
>> print sprintf("%10u\n", $foo);
>>>> Second test, fails due to overflow
>> my $foo = 5188931580; # > 4294967295
>> print sprintf("%10u\n", $foo);
>>>> (actually prints out 4294967295)
>>>>>> My understanding was that a prefix of "L" or "ll" would indicate to sprintf
>> that the value is a "long long" so that any of the following should work:
>>>> print sprintf("%10lld\n", $foo); # Prints -1
>> print sprintf("%10llu\n", $foo); # Print 4294967295
>> print sprintf("%10Ld\n", $foo); # Prints -1
>> print sprintf("%10Lu\n", $foo); # Prints 4294967295
>>>> Have I misunderstood the sprintf syntax or is this a limitation of perl on
>> 32 bit?
>>
> RTFM :)
> q, L or ll interpret integer as C type "long long", "unsigned long long".
> or "quads" (typically 64-bit integers)
>> The last will produce errors if Perl does not understand "quads" in
> your installation.
Fair enough, I should have done that check explicitly. Perl was able to
correctly perform computations on the values though, so it seems the
problem is limited to formatting with sprintf.
Thanks,
-Phil
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!