Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by Marshall (Canon)
on Aug 30, 2021 at 07:50 UTC ( [id://11136196]=note: print w/replies, xml ) Need Help??


in reply to Split any number into string of 8-bit hex values (=1 byte)

Here is another approach for you.
I considered the extra zeroes in for example, "200 => c8 00" to be a bug and I did not attempt to replicate this behavior.
So, here you go, each test number is shown with least significant byte first and also with most significant byte first.

use strict; use warnings; foreach my $num (2, 20, 200, 2000, 20000, 200000) { my $hex_text = sprintf "%x", $num; #use %X for ABCDEF instead of ab +cdef # we are printing bytes, not nibbles, num chars needs to be even $hex_text = "0$hex_text" if (length($hex_text) % 2 == 1); #add lead +ing 0 if odd $hex_text =~ s/([0-9a-fA-F][0-9a-fA-F])(?=[0-9a-fA-F])/$1 /g; my $reversed_bytes = join(" ", reverse split(" ", $hex_text)); print "$num => $reversed_bytes => $hex_text\n"; } __END__ Number => LSB first => MSB first 2 => 02 => 02 20 => 14 => 14 200 => c8 => c8 2000 => d0 07 => 07 d0 20000 => 20 4e => 4e 20 200000 => 40 0d 03 => 03 0d 40
Updated REGEX: instead of a-zA-Z, a-fA-F is adequate
As an additional thought: if you don't need the MSByte first representation, instead of regex, I suppose the split could do more work. If you can explain an algorithmic rule for adding more zeroes, then code could be modified to do that.

A question for the OP:
I am looking at this at 2AM local, but I and all of the other Monks are confused about this:

2 => 2 20 => 14 200 => c8 00 2000 => d0 07 20000 => 20 4e 200000 => 40 0d 03 00
You say that you don't want to have to specify (char, short, long, quad) and that is a good idea because these terms (except in most cases, char) are platform and compiler specific. Why does 20 decimal have a single byte output and 200 decimal have a double byte output? I have no idea! My output shows the single byte that is required in both cases. For 200,000 decimal, I show the necessary 3 bytes. 3 bytes is a "weird duck". These thing usually go like: 1,2,4,8,16 bytes etc. I suppose that there could be a rule such that the code only emits values with those quanta of bytes. However, your example of 2 different number of bytes for 2 and 200 is quite odd. Both values can be represented by a single byte. It is pure speculation on my part, but perhaps, if the most significant bit is "one", then add another byte(s) of zeroes to make it clear that this is not a 2's complement negative number? I have no idea what your intent is. What are the rules (if any) for adding additional zero bytes?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-25 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found