in reply to Split any number into string of 8-bit hex values (=1 byte)
Another approach:
As with LanX's solution,Win8 Strawberry 5.8.9.5 (32) Sun 08/29/2021 18:20:29 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings for my $n (qw(2 20 200 2000 20000 200000 2000000)) { print "$n -> "; print join ' ', unpack '(H2)*', pack 'V', $n; print "\n"; } ^Z 2 -> 02 00 00 00 20 -> 14 00 00 00 200 -> c8 00 00 00 2000 -> d0 07 00 00 20000 -> 20 4e 00 00 200000 -> 40 0d 03 00 2000000 -> 80 84 1e 00
Update: If you're using a 64-bit Perl, you have the 'Q' pack template specifier (update: which uses native endianity by default; see below), so:
(but you get a lot more trailing zeroes :). (Update: The 'Q' specifier should be used with a '<' little-endian specifier to force the desired byte ordering. 'Q' alone - as used in my originally posted code - uses native byte ordering, whatever that may be.)Win8 Strawberry 5.30.3.1 (64) Sun 08/29/2021 20:05:59 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings for my $n (qw(2 20 200 2000 20000 200000 2000000)) { print "$n -> "; # print join ' ', unpack '(H2)*', pack 'Q', $n; # original print join ' ', unpack '(H2)*', pack 'Q<', $n; print "\n"; } ^Z 2 -> 02 00 00 00 00 00 00 00 20 -> 14 00 00 00 00 00 00 00 200 -> c8 00 00 00 00 00 00 00 2000 -> d0 07 00 00 00 00 00 00 20000 -> 20 4e 00 00 00 00 00 00 200000 -> 40 0d 03 00 00 00 00 00 2000000 -> 80 84 1e 00 00 00 00 00
Give a man a fish: <%-{-{-{-<
|
|---|