develooper Front page | perl.beginners | Postings from April 2012

Re: Question on the function - hex

Thread Previous
From:
Uri Guttman
Date:
April 12, 2012 14:27
Subject:
Re: Question on the function - hex
Message ID:
4F8748AF.7050005@stemsystems.com
On 04/12/2012 04:43 PM, Shawn H Corey wrote:
> On 12-04-12 04:34 PM, Parag Kalra wrote:
>> Why does the output of
>> perl -e "print hex '0x00001600000402'"
>>
>> differs from the output of
>> perl -e "print hex 0x00001600000402"
>>
>> EG:
>> bash-3.2$ perl -e "print hex '0x00001600000402'"
>> 94489281538
>>
>> $ perl -e "print hex 0x00001600000402"
>> 10189963531576
>>
>
> $ perl -e "print hex '0x16'"
> 22
> $ perl -e "print hex 0x16"
> 34
> $ perl -e "print hex '22'"
> 34
>
> When inside quotes, '0x16' is interpreted by hex to be a hex string and
> convert to 22. When outside, perl interprets 0x16 as 22 which is passed
> to hex to produce 34.

shawn has it correct here but i want to clarify things a bit. perl will 
see any literal beginning with 0x as a hex number and always convert 
that to an internal integer (no base is used there, but think decimal 
for your brain's sake :). the hex function always takes a string so any 
argument to it will be converted to a base 10 string. as shawn showed, 
that will make 22 decimal become a string and then be converted to 34 
decimal.

the lesson is to be aware of the difference in literals and strings and 
what is doing the parsing and conversion, perl or the hex function.

uri

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About