Solved

URGENT !! Files and Hexadecmial values

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script - Remove column of data based on column value 2 56
Problem with one substitution modifying another 3 78
Perl tar error 8 70
Control Number of Log Files -Perl 7 94
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

730 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