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

Hi Monks, i have many Hex Strings in a array for example: 0FB0306F207265616420746869733F and i will ad the bytes of the string like 0F+B0+30+6F ...and so on, and save the result in Hex to a variable. How can I do this? thanks Tim

Replies are listed 'Best First'.
Re: String adding
by Limbic~Region (Chancellor) on Nov 11, 2005 at 19:10 UTC
    tim0304,
    I think this does what you want.
    my $hex = '0FB0306F207265616420746869733F'; my $sum; $sum += hex($_) for unpack('A2' x (length($hex) / 2), $hex); print $sum;

    Cheers - L~R

      $sum += hex($_) for $hex=~/../g;
      Is a bit less intimidating, IMO.

      Caution: Contents may have been coded under pressure.
      and save the result in Hex to a variable.

      Add:

      $sum_as_hex = sprintf('%X', $sum);
      unpack('A2' x (length($hex) / 2), $hex);

      I'd rewrite that as

      unpack('(A2)*', $hex);

      -sauoq
      "My two cents aren't worth a dime.";
      
        That fails in 5.6.x. The parens were added to pack in 5.8.0
Re: String adding
by BrowserUk (Patriarch) on Nov 11, 2005 at 19:39 UTC

    Sounds like your calculating a checksum?

    perl> $hex = '0FB0306F207265616420746869733F';; perl> print unpack '%32C*', pack 'H*', $hex;; 1329

    And if you need the result in hex

    perl> print unpack 'H*', pack 'N', unpack '%32C*', pack 'H*', $hex;; 00000531

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: String adding
by ikegami (Patriarch) on Nov 11, 2005 at 19:16 UTC
    Alternative:
    use List::Util qw( sum ); my $hex = '0FB0306F207265616420746869733F'; my $sum = sum map hex, $hex =~ /../g; my $sum_as_hex = sprintf '%X', $sum;
Re: String adding
by sgifford (Prior) on Nov 11, 2005 at 19:24 UTC
    You may want to look at using something like bignum with one of the approaches below above. With numbers this large, I believe Perl will convert them into floating-point numbers, and so will lose accuracy.

    The alternative is to handle the carry-out parts yourself, but that's hard, and is essentially what bignum does anyways.

    Update: Oops, I misread the question; I thought you were adding two strings of hex digits, not adding up the hex pairs in one string. Thanks to ikegami and Roy Johnson for showing me the error of my ways. :)

      bignum isn't needed. The example only requires 11 bits, for example.

      Perl uses doubles which have 52 bits of precision. The above algorithms can safely handle strings
      (((2^52)-1)/((2^8)-1))*2 = 17,661,175,009,296 chararacters long.

      Using just ints on a 32 bit system, the above algorithms can safely handle strings
      (((2^32)-1)/((2^8)-1))*2 = 16,843,008 chararacters long.

Re: String adding
by l.frankline (Hermit) on Nov 12, 2005 at 12:21 UTC

    Hi Tim,

    You can also go with this...

    $_ = '0FB0306F207265616420746869733F';
    $_ =~s#([A-F0-9]{2})#$1+#g;
    $_=~s#\+$##;
    print hex($_);


    regards
    Franklin

    Don't put off till tomorrow, what you can do today.