Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

sprintf %X endianness

by Anonymous Monk
on May 14, 2013 at 14:16 UTC ( #1033478=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am after  "00204110" but I'm not sure if the sprintf version is portable

#!/usr/bin/perl -- use Data::Dump; my $raw ="\1\24\2\0" ; dd( 'a is this portable? ', sprintf '%08X', unpack q/L</, $raw); dd( 'b is this portable? ', sprintf '%08X', unpack q/V/, $raw ); dd( 'g is this portable? ', unpack 'H*', pack 'L>', unpack q/L</, $raw + ); __END__ ("a is this portable? ", "00021401") ("b is this portable? ", "00021401") ("g is this portable? ", "00021401")

Also, a way to shorten up the pack/unpack?

Comment on sprintf %X endianness
Select or Download Code
Re: sprintf %X endianness
by BrowserUk (Pope) on May 14, 2013 at 14:41 UTC

    If you know the raw data is little-endian, you can convert it to the current local endianness (without knowing what that is) using:

    my $raw = "\x01\x02\x03\x04";; my $local = unpack 'L<', $raw;;

    Then you can use it as you would any other local variable:

    printf "%08x\n", $local;; 04030201

    Ditto, if you know the raw bytes are big-endian:

    my $raw = "\x01\x02\x03\x04";; my $local = unpack 'L>', $raw;;

    And now when your print it:

    printf "%08x\n", $local;; 01020304

    The point is that once you've done the unpack, it will have been converted to the local format and from that point on you need not consider the endianess; just use it and the code will do the right thing.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      and from that point on you need not consider the endianess . just use it and the code will do the right thing.

      Did I mention I'm after 00204110 ? Does above sprintf return 00204110 on non-win32 machines?

        Is your machine not big or little endian? That's pretty rare hardware.

        You can ask for the values in network order, or worst case, you can unpack it by bytes, and shuffle them manually.

        Does above sprintf return 00204110 on non-win32 machines?

        No. Because your raw input isn't 00204110 as either big-endian or little-endian:

        $raw = "\1\24\2\0";; $local = unpack 'L>', $raw;; printf "%08x\n", $local;; 01140200 $local = unpack 'L<', $raw;; printf "%08x\n", $local;; 00021401

        I don't know of any machine that would represent that hex value with your input bit pattern.

        In fact, I would say it was impossible, because it would mean that the nybbles of the 3rd byte (and only the 3rd byte) would be reversed; and no machine does that!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: sprintf %X endianness
by andal (Friar) on May 15, 2013 at 07:11 UTC

    Very strange question. Function "sprintf" (in C library) interprets the numbers according to the endiannes of the machine. If you put the number in wrong endiannes, then the output of sprintf shall be wrong. I mean, the output of sprintf is text and this text is supposed to be read from left to right.

    How can this be "portable" or "not portable"?

    Normally one needs to worry only about converting sequences of bytes received from external source to the numbers as program understands them. For that the "unpack" function is used. If you know that your external source feeds you with numbers presented as bytes in big-endian order, then you use "N", or "n", or "L>" etc. If you know that it is little-endian, then you use "V", or "v", or "L<" etc. If you don't know what order was used, then you are in trouble :) Once you handle this part correctly, you don't need to worry about sprintf or any other system function anymore.

    Internal numbers will always be handled "correctly" by sprintf. The output of sprintf shall be matching to the big-endian representation of the provided number.

Re: sprintf %X endianness
by ikegami (Pope) on May 15, 2013 at 20:08 UTC

    Endianness is not a factor.

    sprintf %d, %x, %o and %b will always print the most significant digit of the number on the left.

    On all platforms,
    sprintf '%d', 4660 is 4660
    sprintf '%X', 4660 is 1234
    sprintf '%o', 4660 is 11064
    sprintf '%b', 4660 is 1001000110100

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033478]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-10-23 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls