Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: sprintf %X endianness

by BrowserUk (Patriarch)
on May 14, 2013 at 14:41 UTC ( [id://1033489]=note: print w/replies, xml ) Need Help??


in reply to sprintf %X endianness

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.

Replies are listed 'Best First'.
Re^2: sprintf %X endianness
by Anonymous Monk on May 14, 2013 at 19:58 UTC

    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.

        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.

        heh, you mean like I did there with three unpack calls? My question is about how sprintf is supposed to work

      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.

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

        *sigh* look at the op, 00021401 is the thing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1033489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-30 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found