Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Split any number into string of 8-bit hex values (=1 byte)

by tybalt89 (Monsignor)
on Aug 30, 2021 at 13:34 UTC ( [id://11136207]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Split any number into string of 8-bit hex values (=1 byte)
in thread Split any number into string of 8-bit hex values (=1 byte)

I do have another regex ready in case the OP wants answers that are ONLY 1, 2, 4, or 8 bytes long.

We'll just have to wait...

  • Comment on Re^3: Split any number into string of 8-bit hex values (=1 byte)

Replies are listed 'Best First'.
Re^4: Split any number into string of 8-bit hex values (=1 byte)
by drsweety (Novice) on Aug 30, 2021 at 19:25 UTC
    Yes please :-) If I'd convert anything from a quad and then remove unnecessary leading zeros so that I'd end up with 1, 2, 4 or 8 bytes I'm thinking that this should work. What does the regex look like?
      #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11136191 use warnings; for my $n (0, 2, 20, 200, 2000, 20000, 200000) { my @bytes = reverse sprintf('%016X', $n) =~ s/^0{8}(0{4}(00)?)?//r = +~ /../g; print "$n => @bytes\n"; }

      Outputs:

      0 => 00 2 => 02 20 => 14 200 => C8 2000 => D0 07 20000 => 20 4E 200000 => 40 0D 03 00
        Well.... I'm not sure how your regex works BUT it works great for unsigned numbers :-)
        #!/usr/bin/perl use strict; use warnings; sub number2hexString { return my $output .= join ' ', map { join ' ', sprintf('%016X' +, $_) =~ s/^0{8}(0{4}(00)?)?//r =~ /../g } @_; } print number2hexString(2,20,200,2000,20000,200000)."\n";
        It doesn't completely solve my problem as it does not work with unsigned signed numbers

        UPDATE: sorry, I was confused, obviously the last sentence should read ... does not work with signed numbers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found