Or, so that they don't feel left out, pack and unpack:

use MIME::Base64 qw( decode_base64 ); my $scrambled = decode_base64($_); my @scrambled = unpack('C*', $scrambled) ; my @orig = map { $scrambled[$_-1] ^ $scrambled[$_] } 1..$#scram +bled; my $orig = pack('C*', @orig) ;
In this case the magic byte-wise string xor (^) is clearly the best bet. But in general I'd use pack/unpack to do string <-> byte array -- and, of course, so much more.

However, watch out for utf8 strings... unpack('C*', ..) will unpack a utf8 string as characters, so may return values > 0xFF -- so:

my $b = "123\xC4" ; my $u = "a\xC4\x{107}\x{1C4}" ; show(unpack('C*', $b)) ; # 0x31, 0x32, 0x33, 0xC4 show(unpack('C*', $u)) ; # 0x61, 0xC4, 0x107, 0x1C4 { use bytes ; show(unpack('C*', $b)) ; # 0x31, 0x32, 0x33, 0xC4 show(unpack('C*', $u)) ; # 0x61, 0xC3, 0x84, 0xC4, 0x87, 0xC7, 0x +84 } ; sub show { print join(", ", map(sprintf("0x%02X", $_), @_)), "\n" ; } ;
as shown, use bytes causes the unpack to operate on the bytes of encoded form of the utf8 string.

Note that with utf8 strings, pack('C*', ... and unpack('C*', ... are not symmetrical, observe:

my $u = "a\xC4\x{107}\x{1C4}" ; my @u = unpack('C*', $u) ; show(@u) ; # 0x61, 0xC4, 0x107, 0x1C4 bytes(pack('C*',@u)) ; # Character in 'C' format wrapped in pac +k ... # Character in 'C' format wrapped in pac +k ... # 61:C4:07:C4 -- byte sub show { print join(", ", map(sprintf("0x%02X", $_), @_)), "\n" ; } ; sub bytes { my ($s) = @_ ; my $w = utf8::is_utf8($s) ? "utf8" : "byte" ; use bytes ; print join(":", map(sprintf("%02X", $_), unpack('C*', $s))), " -- +$w\n" ; } ;
but this is wandering off topic into a significantly murky area, of possibly marginal utility...


In reply to Re^2: Base64 and byte arrays by gone2015
in thread Base64 and byte arrays by joec_

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.