Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?

In reply to Re: Split any number into string of 8-bit hex values (=1 byte) by Marshall
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found