[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URGENT !! Files and Hexadecmial values

Posted on 2004-04-23
5
Medium Priority
?
263 Views
Last Modified: 2010-03-04
I would like to read any standard ascii file (wether it be a compiled program, image, or text file) and convert it to the actual 'Hex' value.

IE: --
Text string: My name is Slim ;)
# would convert to
Hex value: 4D-79-20-6E-61-6D-65-20-69-73-20-53-6C-69-6D-20-3B-29
# and back to ascii again


Is there any easy way to do this? Maybe a module from cpan, or even better, a built in module ?  I'm experimenting with Perl/Tk I want to make my own version of 'Notepad' because the WindowsXP version is annoying ;)

-- Austin
0
Comment
Question by:Teh_Craze
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10900153
Hi Teh_Craze,
convert a byte value to hex:
$hexbyte = sprintf "%02x", $byte;

...and back:

$number = hex("f7");

Cheers,
Stefan
0
 
LVL 8

Expert Comment

by:davorg
ID: 10900223
Use "pack" and "unpack".

#!/usr/bin/perl
                                                                               
use strict;
use warnings;
                                                                               
my $txt = 'My name is Slim ;)';
                                                                               
my $hex = unpack 'H*', $txt;
                                                                               
print "$hex\n";
                                                                               
my $asc = pack 'H*', $hex;
                                                                               
print "$asc\n";


Dave...
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10900342
Dave,

How to include the dashes in the hex output?

Stefan
0
 
LVL 8

Accepted Solution

by:
davorg earned 2000 total points
ID: 10900380
#!/usr/bin/perl
                                                                               
use strict;
use warnings;
                                                                               
my $txt = 'My name is Slim ;)';
                                                                               
my $hex = unpack 'H*', $txt;
                                                                               
print join '-', $hex =~ /(..)/g;
print "\n";
                                                                               
my $asc = pack 'H*', $hex;
                                                                               
print "$asc\n";
0
 

Author Comment

by:Teh_Craze
ID: 10906875
Brilliant 'davorg'. That was the exact thing I wanted. Thanks. ;)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question