Solved

URGENT !! Files and Hexadecmial values

Posted on 2004-04-23
5
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

691 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