hexadecimal to binary

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

hexadecimal to binary

Post by probkid »

Hi,
I am trying to convert a 34 char. long hexadecimal string to binary and am facing problems. I used base_convert but the string seems to be too large for it. Is there any other way to handle this conversion?
Any help would be greatly appreciated.

Thanks.
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

Check out http://www.php.net/manual/en/function.hexdec.php
especially the comments at the bottom of the page.
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

Post by probkid »

Thanks.
I had checked that link earlier, and if u r talking about the function posted there, I have tried that function. It converts hex to decimal and not binary which was what i was trying. i cannot convert that decimal for my 34 char. string cos it is too large for it to again convert to binary (the fun. posted there returns exponential for me).
do u know of any other possible to work this out?
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Here's something that works. The catch is the number processed has to be a string rather than a number.

Code: Select all

<?php

function hex_digit_2binary($digit)
&#123;
  switch ( $digit ) &#123;
    case '0':
      return '0000';
    case '1':
      return '0001';
    case '2':
      return '0010';
    case '3':
      return '0011';
    case '4':
      return '0100';
    case '5':
      return '0101';
    case '6':
      return '0110';
    case '7':
      return '0111';
    case '8':
      return '1000';
    case '9':
      return '1001';
    case 'a':
      return '1010';
    case 'b':
      return '1011';
    case 'c':
      return '1100';
    case 'd':
      return '1101';
    case 'e':
      return '1110';
    case 'f':
      return '1111';
    default:
      return false;
  &#125;
&#125;

function hex_2bin($hex)
&#123;
  $hex = strtolower($hex);
  $result = "";
  $max = strlen($hex);
  for($i = 0; $i < $max; $i++)
    $result.=hex_digit_2binary($hex&#1111;$i]);
  return $result;
&#125;

?>
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

Post by probkid »

Thanks RandomEngy. I had created a similar function by using base_convert fun. instead of hardcoding all the 15 chars. and it worked. So, that problem was solved. But I ran into another error of PHP while I was trying to convert a decimal string to binary, using base_convert. I was using the string '99999999999998' and got the binary 11001101001100010000011110100011111111111110
when i tried the same conversion in a scientific calculator, it gave me
10110101111001100010000011110100011111111111110

any idea why this would happen???
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

I think your calculator couldn't handle all of the digits somehow. base_convert gave you the right answer.
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

Post by probkid »

I am not sure about that. I used another function after posting the last question and it seemed to return exactly what the calculator returned. I am gonna check in 2 or 3 more ways to finally decide what is right. The fun. is:

function Dec2Bin($number) {
while ($number >= 256) {
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
$number = floor($number / 256);
}
$bytes[] = $number;
for ($i=0;$i<count($bytes);$i++) {
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) :
str_pad(decbin($bytes[$i]), 8, "0", STR_PAD_LEFT)).$binstring;
}
return $binstring;
}
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Sorry bout that, the calculator was right. I just counted 42 digits in the smaller number (base_convert) then calculated 2^43 to be 879... and I thought it was 14 digits, like the input string, but it was actually 13. That led me to conclude the bigger one must be wrong, when it is right.

I actually put the one the calculator gave into Maple 6 (2^1+2^2+...) and got exactly your input string, so base_convert must not have worked.
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

xor function

Post by probkid »

Hi,
Still more probelms with bit manipulation!! :cry:
I'm having some problem using the function "xor" to get xor value of 2 bit strings. The strings that i am using are:
1001100010111111010010000000010010110001110110100100110001101001
and
1000001100111111011010110101000100100001101001111100110010100110
and I am getting the wrong result as
1001100010111111010010000000010010110001110110100100110001101001
whereas it should be
0001101110000000001000110101010110010000011111011000000011001111

Any ideas as to why this happens and what is the way to work around this?

Any help would be greatly appreciated.

Thanks.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

See my reply to your other thread.
Post Reply