Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by drsweety (Novice)
on Aug 30, 2021 at 19:20 UTC ( [id://11136245]=note: print w/replies, xml ) Need Help??


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

First of all I'd like to thank everyone for their contribution! I have realised, that my example and maybe question is unclear and confusing (e.g. regarding endian-ness), sorry about that! Let me try to rephrase my question: I'm looking to split any value/number consisting of x-amount of bytes into single bytes printed in hex and separated by space. So basically I just would like to print a "long" like 200'000 differently: 00 03 0d 40. I don't want to omit any leading zeros in any way, a "long" with 4 bytes should still consist of 4 separates bytes even if 3 would suffice as in my example with 200'000. My example with 200 is wrong as it can be represented by 1 byte (c8) and doesn't need a leading zero, sorry :-(

Anyway, I would then use this code to create a function which takes any number of arguments and returns a string consisting of a series of single bytes represented in hex. The only thing I came up with is this:

#!/usr/bin/perl

sub number2hexString {
	my $output;
	my $packTemplate;
	foreach my $i (@_) {
		if ($i > 65535) {
			$packTemplate = 'L>';
		} elsif ($i > 255) {
			$packTemplate = 'S>';
		} else {
			$packTemplate = 'C';
		}
		$output .= join(' ', unpack('(H2)*', pack($packTemplate, $i))).' ';
	}
	return $output;
}

print number2hexString(2,20,200,2000,20000,200000)."\n";
this results in:
$ ./test.pl
02 14 c8 07 d0 4e 20 00 03 0d 40
$

This works. However, I don't need perl to know the type of number it needs to convert. It shouldn't care whether it's a char, short, long, quad, signed or unsigned or whatever. It should just take each variable as a series of bytes and convert it to single bytes represented in hex. Basically I'm looking to replace the if/elsif/else part in the above sub.I'm hoping this clears things up!

Background: I'm using this to communicate via I2C (a slow serial hardware bus) between a Raspberry and an Arduino. As it is slow I do not want to waste unnecesseray bytes (otherwise sending everything as a quad or long would be an option). And as I'm handling the Arduino side as well I know which datatype I'm expecting based on the register and can then reassemble my series of bytes into single bytes, ints or longs etc. A sample transmission would be: master sends: 02 14 07 d0 which the slave (arduino) interprets as follows:

  • 0x02 == 2 = I2C address of the Arduino.
  • 0x14 == 20 = I2C register (which in this eample expects a short. But it could also be a long or anything else. The Arduino-code will handle it)
  • 0x07 and 0xd0 == 2000 = The value to be written into the I2C register
  • Comment on Re: Split any number into string of 8-bit hex values (=1 byte)

Replies are listed 'Best First'.
Re^2: Split any number into string of 8-bit hex values (=1 byte)
by LanX (Saint) on Aug 30, 2021 at 21:32 UTC
    > However, I don't need perl to know the type of number it needs to convert.

    that doesn't sound right ...

    If you have the value 2 which has to be put into a long register as 00 00 00 02 how is Perl supposed to guess that 4 bytes are needed and 02 isn't already sufficient?

    But if your transfer protocol was able to handle 02 for a long register, why would anyone need to stuff any leading/trailing 0s into it?

    The straight mathematical way to tell the "width" of a number is using log like already shown. But given your demonstrated code I'd rather suggest you sticking with 3 if-then-else levels inside a sub.

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

Log In?
Username:
Password:

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

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

    No recent polls found