Jeff Westman wrote:
> 
> Hi,

Hello,

> 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:
> 
> #!/bin/perl
> 
> use strict;
> use warnings;
> 
> my $str = "some string";
> my $hex = unpack('H*', "$str");
> 
> my $len = length($hex);
> my $start = 0;
> 
> print "x'";
> while ($start < $len) {
>     print substr($hex,$start,2);
>     $start += 2;
> }
> print "'\n";
> 
> Produces:
> x'736f6d6520737472696e67'

One way to do it:

my $str = 'some string';

$str =~ s/(.)/ sprintf '%02x', ord $1 /seg;

print "x'$str'\n";



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to