views:

111

answers:

2

Hey everyone,

I have a string that looks like this "7a" and I want to convert it to the hex number 7A. I have tried using pack and unpack but that is giving me the hex representation for each individual character. I'm out of ideas and don't know where to go from here.

Thanks for the help!

+12  A: 

Probably the simplest way to store that as an integer is hexdec()

$num = hexdec( '7A' );
Peter Bailey
I was going to suggest sscanf, but this is better.
Artefacto
Thanks this is exactly what I wanted. I saw this function earlier but I misunderstood what is did so I thought it wasn't what I wanted.
TheGNUGuy
+1  A: 

Well a number is a number, it does not depend on the representation. You can get the actual value using intval():

$number = intval('7a', 16); 

To convert the number back to a hexadecimal string you can use dechex().

Felix Kling