hexadecimal to binary
Moderator: General Moderators
hexadecimal to binary
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.
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.
Check out http://www.php.net/manual/en/function.hexdec.php
especially the comments at the bottom of the page.
especially the comments at the bottom of the page.
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?
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?
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
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)
{
switch ( $digit ) {
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;
}
}
function hex_2bin($hex)
{
$hex = strtolower($hex);
$result = "";
$max = strlen($hex);
for($i = 0; $i < $max; $i++)
$result.=hex_digit_2binary($hexї$i]);
return $result;
}
?>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???
when i tried the same conversion in a scientific calculator, it gave me
10110101111001100010000011110100011111111111110
any idea why this would happen???
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
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;
}
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;
}
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact:
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.
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.
xor function
Hi,
Still more probelms with bit manipulation!!
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.
Still more probelms with bit manipulation!!
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.
- RandomEngy
- Forum Contributor
- Posts: 173
- Joined: Wed Jun 26, 2002 3:24 pm
- Contact: