Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a string that looks like a hex number to an actual hex number in php?

Tags:

string

php

hex

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.

like image 687
cskwrd Avatar asked May 19 '10 14:05

cskwrd


2 Answers

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

$num = hexdec( '7A' );
like image 159
Peter Bailey Avatar answered Sep 20 '22 02:09

Peter Bailey


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().

like image 44
Felix Kling Avatar answered Sep 19 '22 02:09

Felix Kling