Link to home
Start Free TrialLog in
Avatar of Joe Weinpert
Joe Weinpert

asked on

PHP - How can I convert a large hexadecimal value to a binary value?

PHP - How can I convert a large hexadecimal value to a binary value?

<?php
   $hexString = "100000000000000000";
   echo base_convert( $hexString, 16, 2 );
?>

Open in new window

Is returning a 64 character long string of zeros "0000000000000000000000000000000000000000000000000000000000000000"
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

You need to use hex2bin();  Pass in your hex number and it will convert it to binary
Avatar of Joe Weinpert
Joe Weinpert

ASKER

I tried that prior to the base_convert() function and it also does not work.  In fact, the PHP documentation says the following about the hex2bin() function:

CAUTION This function does NOT convert a hexadecimal number to a binary number. This can be done using the base_convert() function.

Open in new window

You got the order wrong.  This works.
<?php
   $hexString = "100000000000000000";
   echo base_convert( $hexString, 2, 16 );
?>

Open in new window

The order I placed the parameters is correct (from the PHP manual:

string base_convert ( string $number , int $frombase , int $tobase )

Open in new window

I am converting a hex value of "100000000000000000" to a binary value
Ok, I rewrote my test program to include a lot of known values working up to your desired input.  It appears that you are exceeding the resolution of the function.
<?php
   $hexString = "1";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "2";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "4";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "8";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "A";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "F";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "10";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "100";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "1000";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "10000";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "1000000";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "100000000";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "1000000000000000";
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
   $hexString = "100000000000000000"; // your number
   echo "$hexString H = ".base_convert( $hexString, 16, 2 )." B<br>";
?>

Open in new window

This page has comments about exceeding the resolution.  http://php.net/manual/en/function.base-convert.php
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks NerdsOfTech
Thank you. I cleaned the code up a bit further in case you want a UDF for this task (TESTED):

function hexbin64plus($str, $reverse = 0){
 $str = preg_replace("/[^0-9A-Fa-f]/", "", $str); // sanitize all non-hex chars
 if(preg_match("/[a-f]/i", $str) == 1){ // hex detected
   $str = strtolower($str); // coalesce A-F to a-f
 }
 if($reverse == 0){ // hex to bin (default)
  $chunk = 1;
  $frombase = 16;
  $tobase = 2;
  $format = '%04d';
 }else{ // bin to hex (reverse flag is on)
  $chunk = 4;
  $frombase = 2;
  $tobase = 16;
  $format = '%s';
 }
 $chars = str_split($str, $chunk);
 $str = '';
 foreach($chars as $char){
  $char = sprintf($format, base_convert($char, $frombase, $tobase));
  $str .= $char;	
 }
 return $str;
}

$hex = $bin = '';
$hex = '100000000000000000';
echo $hex . " H <BR>";
$bin = hexbin64plus($hex);
echo ltrim($bin, '0') . " B" . "<BR>";
echo strlen(ltrim($bin, '0')) . " bits" . "<BR>";

// reverse the conversion
$hex = hexbin64plus($bin, 1);
echo strtoupper($hex) . " H" . "<BR>";
echo "<HR><BR>";

//try another conversion
$hex = '2A';
echo $hex . " H <BR>";
$bin = hexbin64plus($hex);
echo ltrim($bin, '0') . " B" . "<BR>";
echo strlen(ltrim($bin, '0')) . " bits" . "<BR>";

// reverse the second conversion
$hex = hexbin64plus($bin, 1);
echo strtoupper($hex) . " H" . "<BR>";

Open in new window