Solved

URGENT !! Files and Hexadecmial values

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 87
How to send output of ping to syslog (with timestamp and optional comments) 3 191
PERL variable conundrum 9 89
Perl modules on linux ec2 3 96
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

17 Experts available now in Live!

Get 1:1 Help Now