Help with hex

Help with hex

am 04.04.2009 01:07:46 von Barry Brevik

I am running Active Perl 5.8.8 on Windows and am writing a data
conversion app into which I want to build some debug routines.

To that end, I want to add a routine to take any string and display it
as hex numbers.

The string can be anywhere from 0 (zero) to over 1,000 characters and
can contain any byte value from 00 to ff.

I want to do this without adding the overhead of printf or sprintf to my
program. Has anyone out there done this before?

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with hex

am 04.04.2009 02:21:07 von Williamawalters

--===============1822770776==
Content-Type: multipart/alternative;
boundary="part1_cda.48d8fb29.370801f3_boundary"


--part1_cda.48d8fb29.370801f3_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

hi barry --

In a message dated 4/3/2009 6:08:37 PM Eastern Standard Time,
BBrevik@StellarMicro.com writes:

> To that end, I want to add a routine to take any string and display it
> as hex numbers.
> The string can be anywhere from 0 (zero) to over 1,000 characters and
> can contain any byte value from 00 to ff.

the unpack() function is your friend in this situation, and specifically
the 'C*' unpack template specifier. after you have unpack()-ed the
string to an array (and an array of 1000 bytes is not terribly big), it
is just a matter of printing the array in the format you find most
helpful.

> I want to do this without adding the overhead of printf or sprintf to
> my program.

i am not sure what you mean by this. the printf() and sprintf()
functions are part of the perl dll and cannot be removed without
re-compilation, which i assume you have not done since a lot of
stuff would break if you did.
can you be more specific on this point?

br -- bill walters


**************
Hurry! April 15th is almost here. File your Federal taxes FREE
with TaxACT.
(http://pr.atwola.com/promoclk/100126575x1220714320x12013676 38/aol?redir=http:%2F%2Fwww.taxact.com%2F08tax.asp%3Fsc%3D08 4102950001%26p%3D82)

--part1_cda.48d8fb29.370801f3_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

hi barry -- &=
nbsp; 



In a message dated 4/3/2009 6:08:37 PM Eastern Standard Time, BBrevik@St=
ellarMicro.com writes:



> To that end, I want to add a routine to take any string and display=
it

> as hex numbers.

> The string can be anywhere from 0 (zero) to over 1,000 characters a=
nd

> can contain any byte value from 00 to ff.



the unpack() function is your friend in this situation, and specifically=
=20

the 'C*' unpack template specifier.   after you have unpack()-=
ed the=20

string to an array (and an array of 1000 bytes is not terribly big), it=20

is just a matter of printing the array in the format you find most=20

helpful.   



> I want to do this without adding the overhead of printf or sprintf=20=
to

> my program.   



i am not sure what you mean by this.   the printf() and sprint=
f()=20

functions are part of the perl dll and cannot be removed without=20

re-compilation, which i assume you have not done since a lot of=20

stuff would break if you did.   

can you be more specific on this point?   



br -- bill walters   



**************
Hurry! April 15th is almost here. File=20=
your Federal taxes FREE with TaxACT. (http://pr.atwola.com/promoclk/10012657=
5x1220714320x1201367638/aol?redir=3Dhttp:%2F%2Fwww.taxact.co m%2F08tax.asp%3F=
sc%3D084102950001%26p%3D82)

--part1_cda.48d8fb29.370801f3_boundary--

--===============1822770776==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1822770776==--

Re: Help with hex

am 04.04.2009 03:21:58 von Williamawalters

--===============1919826933==
Content-Type: multipart/alternative;
boundary="part1_c90.3ec2694b.37081036_boundary"


--part1_c90.3ec2694b.37081036_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

In a message dated 4/3/2009 7:23:43 PM Eastern Standard Time,
BBrevik@StellarMicro.com writes:

> > i am not sure what you mean by this. the printf() and sprintf()
> > functions are part of the perl dll and cannot be removed without ...
> > can you be more specific on this point?
>
> No, I just assumed that it added overhead, like with 'C'.

if it's a straightforward script we're talking about, then as far as
i am aware, the only overhead involved would be the executional
overhead of actually calling one of the functions in question.

br -- bill walters


**************
Hurry! April 15th is almost here. File your Federal taxes FREE
with TaxACT.
(http://pr.atwola.com/promoclk/100126575x1220714320x12013676 38/aol?redir=http:%2F%2Fwww.taxact.com%2F08tax.asp%3Fsc%3D08 4102950001%26p%3D82)

--part1_c90.3ec2694b.37081036_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

In a message=20=
dated 4/3/2009 7:23:43 PM Eastern Standard Time, BBrevik@StellarMicro.com wr=
ites:



> > i am not sure what you mean by this. the printf() and sprintf(=
)

> > functions are part of the perl dll and cannot be removed witho=
ut ...

> > can you be more specific on this point?

>=20

> No, I just assumed that it added overhead, like with 'C'.



if it's a straightforward script we're talking about, then as far as=20

i am aware, the only overhead involved would be the executional=20

overhead of actually calling one of the functions in question.  &nb=
sp;



br -- bill walters   



**************
Hurry! April 15th is almost here. File=20=
your Federal taxes FREE with TaxACT. (http://pr.atwola.com/promoclk/10012657=
5x1220714320x1201367638/aol?redir=3Dhttp:%2F%2Fwww.taxact.co m%2F08tax.asp%3F=
sc%3D084102950001%26p%3D82)

--part1_c90.3ec2694b.37081036_boundary--

--===============1919826933==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1919826933==--

Re: Help with hex

am 04.04.2009 03:30:29 von Williamawalters

--===============0835220048==
Content-Type: multipart/alternative;
boundary="part1_ce8.4fe5ccb4.37081235_boundary"


--part1_ce8.4fe5ccb4.37081235_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

In a message dated 4/3/2009 6:08:37 PM Eastern Standard Time,
BBrevik@StellarMicro.com writes:

> I want to add a routine to take any string and display it as hex numbers.


btw -- i would be surprised if there was not at least one hex-dump module
in the activestate or cpan archive. have you checked? you might
save yourself some trouble.

br -- bill walters


**************
Hurry! April 15th is almost here. File your Federal taxes FREE
with TaxACT.
(http://pr.atwola.com/promoclk/100126575x1220714320x12013676 38/aol?redir=http:%2F%2Fwww.taxact.com%2F08tax.asp%3Fsc%3D08 4102950001%26p%3D82)

--part1_ce8.4fe5ccb4.37081235_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

In a message=20=
dated 4/3/2009 6:08:37 PM Eastern Standard Time, BBrevik@StellarMicro.com wr=
ites:



> I want to add a routine to take any string and display it as hex nu=
mbers.





btw -- i would be surprised if there was not at least one hex-dump modul=
e=20

in the activestate or cpan archive.   have you checked?  =
 you might=20

save yourself some trouble.   



br -- bill walters   



**************
Hurry! April 15th is almost here. File=20=
your Federal taxes FREE with TaxACT. (http://pr.atwola.com/promoclk/10012657=
5x1220714320x1201367638/aol?redir=3Dhttp:%2F%2Fwww.taxact.co m%2F08tax.asp%3F=
sc%3D084102950001%26p%3D82)

--part1_ce8.4fe5ccb4.37081235_boundary--

--===============0835220048==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0835220048==--

RE: Help with hex

am 06.04.2009 13:02:52 von Brian Raven

Barry Brevik <> wrote:
> I am running Active Perl 5.8.8 on Windows and am writing a data
> conversion app into which I want to build some debug routines.
>
> To that end, I want to add a routine to take any string and display
> it as hex numbers.
>
> The string can be anywhere from 0 (zero) to over 1,000 characters and
> can contain any byte value from 00 to ff.
>
> I want to do this without adding the overhead of printf or sprintf to
> my program. Has anyone out there done this before?

Probably lots of times. I am not aware of any modules that do this, but
that may be because it is fairly trivial to do in a simplistic way, e.g.

print unpack("H*", $string), "\n";

I also have a function that produces more readable output lying around
that you are welcome to, if it helps, although it uses sprintf. It could
almost certainly be made more efficient, but only if necessary.

# Dump a message in hex (looks a bit like emacs hexl-mode). Parameters
# are the message to dump, and an optional bytes per line (default
# 16). Returns the dumped string ready to print.
sub hex_dump_msg {
my $msg = shift;
my $bpl = shift || 16;
return "" unless defined $msg and $msg ne "";
$bpl > 0 && ($bpl % 2) == 0
or die "Invalid bytes per line ($bpl). Should be positive and
even.\n";
my $res = "";
my $addr = 0;
while ($msg ne "") {
my $seg = substr($msg, 0, $bpl);
substr($msg, 0, $bpl) = "";
my @words = unpack "H4" x ($bpl / 2), $seg;
$res .= sprintf "%04x: ", $addr;
$res .= join(" ", map {pad4($_)} @words);
$seg =~ s/[[:^print:]]/./g;
$res .= " $seg\n";
$addr += $bpl;
}
return $res;
}

sub pad4 {
my $str = shift || "";
return substr("$str ", 0, 4);
}

Only worry about the cost of using [s]printf when it becomes more than
you are prepared to pay.

HTH

--
Brian Raven

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs