Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anyone have a ascii/Hex converter?

Status
Not open for further replies.

jerehart

Programmer
Jun 10, 1999
61
US
Hey I am just looking to transform an ascii string into the hex equivalent string

'hi' => 0x6869

I figure this has been done and too lazy to do it today.
 
Perl has the builtin function unpack() to do just this.

$string = "Hello, World!";
print "0x",unpack("H*",$string),"h";

returns:
0x48656c6c6f2c20576f726c6421h

Or if you want to do it on a character by character basis:

$string = "Hello, World!";
@array = split //,$string;
while(@array) {
print "0x",unpack("H2",shift),"h";
}

Hope this helps,

brendanc@icehouse.net
 
Great answer sophisticate. I knew that pack or unpack should be able to do it, but I've never really understood pack and unpack as well as I should. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Ya dude thanks that is totally what I needed. I knew there was an easier way to do it

Cheers dude

Miah
 
Well if I try and do it for each character all I get out are a bunch of

0xh0xh0xh all over the screen any ideas about that?
 
Here's another way to do it character by character:
Code:
foreach my $char (split(//, $string)) {
   print "0x" . unpack("H2",$char) . "h";
}
A side comment: I think using commas in the print statement will separate the parts with spaces, whereas this definitely won't.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I've got to stop writing these posts at 4am.

In the while(@array) loop, there's a shift in there... as I wrote it, it's shifting nothing. So change that to shift(@array) and you'll be fine.
Code:
$string = "Hello, World!";
@array = split //,$string;
while(@array) {
    print "0x",unpack("H2",shift(@array)),"h";
}

brendanc@icehouse.net
 
SCHWEEET! that got it. thanks.

I know a lot but need to still learn, not to used to using shift and un/pack. Better start using the Cookbook more often.

Thanks again.
 
By the way, the comma operator is more efficient than the concatenation (.) operator in print statements because it's simply printing value1 then value2 then value3 instead of appending value1 with the proceeding values and writing the new string. Concatenation works if you're storing a string in a variable, but probably shouldn't be used in prints. And there's no risk of extra spaces.

It's not much of a boost, but still...

brendanc@icehouse.net
 
You are correct about the comma operator on the print statement. I just tested it to make sure. I guess I was thinking about other languages where a comma inserts a space or a tab or some separator character.

I stand (humbly) corrected. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top