Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Byte-order and packing numbers into strings

by larryk (Friar)
on Jan 24, 2002 at 21:53 UTC ( #141258=perlquestion: print w/ replies, xml ) Need Help??
larryk has asked for the wisdom of the Perl Monks concerning the following question:

The Situation
I need to format an integer into an 8-byte hexadecimal string as follows: if hex representation is (st uv wx yz) the formatted string must be (yz wx uv st st uv wx yz).

e.g. 305419896 in hex is (12345678) so formatted string should be (78 56 34 12 12 34 56 78)

The Question
although this works I have a nagging suspicion that this isn't The Best Way (tm) - is there some way to incorporate the reversing (little/big endian or something) into the pack/sprintf somehow - pack is still mysterious to me!

The Code

#!perl use strict; use warnings; sub both_byte_orders_32 { my $data = shift; $data = pack 'H*', sprintf '%08x', $data; return reverse($data).$data } print both_byte_orders_32(305419896);

Any help much appreciated,

   larryk                                          
perl -le "s,,reverse killer,e,y,rifle,lycra,,print"

Comment on Byte-order and packing numbers into strings
Download Code
Re: Byte-order and packing numbers into strings
by demerphq (Chancellor) on Jan 24, 2002 at 22:13 UTC
    This seems to work. At least it produces the same as your code...
    sub bbo_32 {pack'VN',$_[0],$_[0]}
    update:
    When I first started out with perl I found that pack and unpack were menacing frightful things that I was never willing to spend time to grok. However it turns out that they are a lot easier than they look, just take the time to mess around with em. Very useful in a lot of places and ways. Which is not to say that I know them that well. I still use perlfunc pretty much every time I need to use either.

    Yves / DeMerphq
    --
    When to use Prototypes?

      To further golf this, you could do the following:
      sub bbo_32 {pack'VN',@_[0,0]}
      Which is a low-tech add on to an elegant solution by demerphq.

      Update: demerphq points out $_[0,0] to the correct @_[0,0]. Cut and paste error.
        Nice! ++ for sure! (Well, overlooking that it should be @_[0,0] ;-)

        Similar in length is also

        sub bbo_32{pack'VN',(pop)x2}

        Yves / DeMerphq
        --
        When to use Prototypes?

Re: Byte-order and packing numbers into strings
by theorbtwo (Prior) on Jan 25, 2002 at 09:48 UTC

    Just wondering: what do you need this for? I know numbers like this are used internaly in some peices of ISO-9660 (and iso9660-like, like playstation) filesystems, but why do you need to print out human-readable hex in this format? Or am I smoking crack?

    (I'm interested in what you're doing even if I am smoking crack. I recently tried to divine the ps1 diskformat by example (for no good reason, since I was looking for PS2 data, and it uses a different (DVD-based) disk format), but ended up using C instead, since that way I could steal more easily from the Linux iso9660 code.

    TACCTGTTTGAGTGTAACAATCATTCGCTCGGTGTATCCATCTTTG ACACAATGAATCTTTGACTCGAACAATCGTTCGGTCGCTCCGACGC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://141258]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-30 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls