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?? |
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.
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: 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 Section
Seekers of Perl Wisdom
|
|