Ascii to hex?

B

Bert

How do I convert an ascii to hex? I want the hex stored in a variable, not
just printed out.
For example,
my testchars = 'CICS003'
How do I store that value in hexchars?
I know this is basic, but it eludes me...
Thanks from a newbie.
 
J

Jim Gibson

Bert said:
How do I convert an ascii to hex? I want the hex stored in a variable, not
just printed out.
For example,
my testchars = 'CICS003'
How do I store that value in hexchars?
I know this is basic, but it eludes me...
Thanks from a newbie.

You can use ord to get the numerical value of an ascii character and
sprintf "%x" to generate the hexadecimal representation of that number:

#!/opt/perl/bin/perl
use strict;
use warnings;

my $testchars = 'CICS003';
my $hexchars = '';
foreach my $c (split(//,$testchars)) {
$hexchars .= sprintf "%x", ord($c);
}
print "$hexchars\n";

__OUTPUT__
43494353303033

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top