Solved

URGENT !! Files and Hexadecmial values

Posted on 2004-04-23
5
208 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now