Here a more generic solution, the grouping is free now

(left the reverse out for better testing)

use strict; use warnings; use feature "say"; sub hexgroups { my ( $n, $group ) = @_; die "Can't handle negative $n" if $n <0; $group //= 2; # default 1 byte = 2 nibbles my $nibbles = $n ? int( log($n)/log 16**$group ) +1 : 1 ; # 00 has no log $nibbles *= $group; # zero padding my @list = sprintf( '%0*x', $nibbles, $n ) =~ /(.{$group})/g; return @list; } for my $n (0, map { 2*"1e$_"} 0..10) { say "$n => "; say "\t"x2, join " ", hexgroups($n, $_) for 1,2,3,4; }

C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/hex_groups.pl 0 => 0 00 000 0000 2 => 2 02 002 0002 20 => 1 4 14 014 0014 200 => c 8 c8 0c8 00c8 2000 => 7 d 0 07 d0 7d0 07d0 20000 => 4 e 2 0 4e 20 004 e20 4e20 200000 => 3 0 d 4 0 03 0d 40 030 d40 0003 0d40 2000000 => 1 e 8 4 8 0 1e 84 80 1e8 480 001e 8480 20000000 => 1 3 1 2 d 0 0 01 31 2d 00 001 312 d00 0131 2d00 200000000 => b e b c 2 0 0 0b eb c2 00 00b ebc 200 0beb c200 2000000000 => 7 7 3 5 9 4 0 0 77 35 94 00 077 359 400 7735 9400 20000000000 => 4 a 8 1 7 c 8 0 0 04 a8 17 c8 00 4a8 17c 800 0004 a817 c800 Compilation finished at Mon Aug 30 16:28:18

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re^3: Split any number into string of 8-bit hex values (=1 byte) by LanX
in thread Split any number into string of 8-bit hex values (=1 byte) by drsweety

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.