Using Pack and Unpack

A perl program needs to send binary data to an external device
such that 0xff looks like 11111111

	I have a line in the program as follows:

my $txstart = pack("h*","fefe5a95");

Are those 4 bytes usable as the binary data 

fe fe 5a 95?

	Is there a good way when running perl -d to view the
contents of the string to make sure it is what it should be?

Thank you.

Martin McCormick
0
martin
12/7/2017 3:53:37 AM
perl.beginners 29264 articles. 3 followers. Follow

3 Replies
6 Views

Similar Articles

[PageSpeed] 42

On Wed, 2017-12-06 at 21:53 -0600, Martin McCormick wrote:
> A perl program needs to send binary data to an external device
> such that 0xff looks like 11111111


$ perl -le'my $x = sprintf q/%b/, 0xff; print $x'
11111111



> 	I have a line in the program as follows:
> 
> my $txstart = pack("h*","fefe5a95");
> 
> Are those 4 bytes usable as the binary data 
> 
> fe fe 5a 95?
> 
> 	Is there a good way when running perl -d to view the
> contents of the string to make sure it is what it should be?


use Data::Dumper;
$Data::Dumper::Useqq = 1;

my $txstart = pack 'h*', 'fefe5a95';

print Dumper $txstart;



John
0
jwkrahn
12/7/2017 6:17:32 AM
On Wed, 06 Dec 2017 21:53:37 -0600
"Martin McCormick" <martin.m@suddenlink.net> wrote:

> A perl program needs to send binary data to an external device
> such that 0xff looks like 11111111
>=20
> 	I have a line in the program as follows:
>=20
> my $txstart =3D pack("h*","fefe5a95");
>=20
> Are those 4 bytes usable as the binary data=20
>=20
> fe fe 5a 95?
>=20
> 	Is there a good way when running perl -d to view the
> contents of the string to make sure it is what it should be?
>=20

Yes, see the "x" command
( http://perl-begin.org/tutorials/perl-for-newbies/part2/#page--debugger--D=
IR ),
https://metacpan.org/pod/Data::Dumper , https://metacpan.org/pod/DDP , and =
some
debugger GUIs such as https://en.wikipedia.org/wiki/Data_Display_Debugger .

Regards,

	Shlomi

> Thank you.
>=20
> Martin McCormick
>=20



--=20
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

Open source software: each person contributes a brick, but ultimately each
person receives a house in return.
    =E2=80=94 Brendan Scott (Attributed)

Please reply to list if it's a mailing list post - http://shlom.in/reply .
0
shlomif
12/7/2017 9:12:55 AM
"John W. Krahn" <jwkrahn@shaw.ca> writes:
> 
> $ perl -le'my $x = sprintf q/%b/, 0xff; print $x'
> 11111111
> 

> use Data::Dumper;
> $Data::Dumper::Useqq = 1;
> 
> my $txstart = pack 'h*', 'fefe5a95';
> 
> print Dumper $txstart;

Thank you and Shlomi Fish.  I don't know when one stops being a
beginner in perl because there are so many different things one
can do with it.  At least I know that this project will work
because I am revisiting a similar project I did in 2008 when I
wrote a program in gcc C to communicate with the same radio
receiver.  It worked but with perl, one does not have to
re-invent so many wheels just to get a few string functions to
behave.  Electronics enthusiasts will understand this next
sentence.  Perl is like the difference between building a circuit
with discrete components and building it modularly with
integrated circuits that have years of development baked in.  If
you understand what the IC's do, you can really make something to
be proud of and be pretty sure that it will not give you any
nasty surprises when operating conditions change.

Again, thanks to all.

Martin McCormick WB5AGZ
0
martin
12/7/2017 1:37:44 PM
Reply: