donfreenut has asked for the wisdom of the Perl Monks concerning the following question:


Yes, I've got another hexadecimal question.

How do I take a hexadecimal string of 2, 3 or 4 bytes and combine them into one number?

i.e. I have the hex string x2D x05 and I want to make x2D05, or 11,525. I've tried packing and unpacking every way I know how (which is about 1 way).

Thanks again...
---
donfreenut
  • Comment on Constructing 32-bit hexadecimal integers

Replies are listed 'Best First'.
Re: Constructing 32-bit hexadecimal integers
by I0 (Priest) on Apr 12, 2001 at 00:30 UTC
Re: Constructing 32-bit hexadecimal integers
by Rudif (Hermit) on Apr 13, 2001 at 02:18 UTC
    I've tried packing and unpacking every way I know how ...

    Last night, I was hacking, packing and unpacking because my understanding of those operations was lacking.

    I hope that you will find my examples below of some help.
    Bonus question: is my machine big-endian or little-endian?

    Rudif

    #! perl -w use strict; my $string = uc " hack && pack || unpack"; #my $string = 'abcdefgh'; #my $string = uc 'abcdefgh'; #my $string = 'ABCD'; my @hex = unpack 'H*', $string; print "H @hex\n"; my @bytes = unpack 'C*', $string; print "C @bytes\n"; my @revbytes = reverse @bytes; my $revbytes = pack 'C*', @revbytes; print "C $revbytes\n"; my @shorts = unpack 'S*', $string; print "S @shorts\n"; my @revshorts = reverse @shorts; my $revshorts = pack 'S*', @revshorts; print "S $revshorts\n"; my @longs = unpack 'L*', $string; print "L @longs\n"; my @revlongs = reverse @longs; my $revlongs = pack 'L*', @revlongs; print "L $revlongs\n"; my @bits = unpack 'B*', $string; print "B @bits\n"; my @revbits = reverse @bits; my $revbits = pack 'B*', @revbits; print "B $revbits\n"; ++$_ foreach @bytes; my $bytes = pack 'C*', @bytes; print "C $bytes\n"; __END__ H 204841434b202626205041434b207c7c20554e5041434b C 32 72 65 67 75 32 38 38 32 80 65 67 75 32 124 124 32 85 78 80 65 6 +7 75 C KCAPNU || KCAP && KCAH S 18464 17217 8267 9766 20512 17217 8267 31868 21792 20558 17217 S ACNP U||K AC P&&K AC H L 1128351776 640032843 1128353824 2088509515 1347310880 L UNPK || PACK && HAC B 001000000100100001000001010000110100101100100000001001100010011000 +100000010100000100000101000011010010110010000001111100011111000010000 +0010101010100111001010000010000010100001101001011 B HACK && PACK || UNPACK C !IBDL!''!QBDL!}}!VOQBDL
Re: Constructing 32-bit hexadecimal integers
by satchboost (Scribe) on Apr 11, 2001 at 23:40 UTC
    try something like:
    my $first = 0x2d; my $second = 0x05; my $string = sprintf("%02X%02X", hex($first), hex($second));

    The basic idea is to sprintf() them, then concatenate the resultant strings.


      I didn't explain this very clearly, sorry.

      A set of 1, 2, 3 or 4 hex values are stored in one variable. If you printed the variable, you'd get whatever ASCII characters corresponded to the hex values. (There's probably a better way to explain this, but I don't really speak the language quite yet.)

      Should I split() the variable, then sprintf, then concatenate?

      Thanks once more...

      ---
      donfreenut
        Nah, use unpack at will.

        You will have to pad them missing values:

        $a = "\001"; #just another hex string $a = ("\000" x (4-length($a))).$a; #Zero pad that string print unpack 'L', $a; #Hey, that's one!

        Jeroen
        "We are not alone"(FZ)

Re: Constructing 32-bit hexadecimal integers
by yakko (Friar) on Apr 12, 2001 at 00:27 UTC
    Here's a way:
    print hex(c030), "\n"; # prints "49200"
    perldoc -f hex

    Update: actually, on closer examination, you want to actually catenate 8-bit values into a big hex string. If this is the case, jeroenes has answered that above... I worked on a program awhile back in which I used something like:

    $aschex.=uc(sprintf("%02x",ord($rawhex)));
    ... to get bytes that were raw hex values to display as an ASCII representation of hex.

    --
    Me spell chucker work grate. Need grandma chicken.