James Edward Gray II <[EMAIL PROTECTED]> wrote:
> On Nov 18, 2003, at 10:33 AM, Jeff Westman wrote:
>
> > There must be an easier way to convert a basic ascii string to hex. I
> > tried
> > using ord/chr/unpack/sprintf(%x) combinations and just dug my hole
> > deeper.
> > I'm not interested in using any additional modules, just straight,
> > "basic"
> > perl. This works, but exactly elegant:
>
> This one-liner produces identical output:
>
> perl -e 'print "x", unpack("H*", "some string"), "\n"'
Looks great. How could I have missed that?!
> That doesn't seem too complex, does it?
>
> Hmm, let's take a look...
>
> > #!/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > my $str = "some string";
> > my $hex = unpack('H*', "$str");
>
> The line above is 100% of the hex conversion. That's too cumbersome???
> I doubt we can do much better.
>
> > my $len = length($hex);
> > my $start = 0;
> >
> > print "x'";
> > while ($start < $len) {
> > print substr($hex,$start,2);
> > $start += 2;
> > }
> > print "'\n";
>
> Ah, the long part! Printing two characters at a time. Any reason to
> do this?
Originally, I wanted to print individually ASCII characters into hex, such
as,
x'73' x'6f' x'6d' (etc)
> Well, surely we can do it quicker:
>
> print 'x';
> for (my $i = 0; $i < length $hex; $i+=2) {
> print substr $hex, $i, 2;
> }
> print "\n";
>
> Is that better? I'll let you decide. It's pretty C-looking, but that
> probably doesn't have to be a bad thing.
>
> Any of this help?
Yep, thanks!
-Jeff
__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]