-
I need PHP help.
function hex2bin($str) {
$bin = "";
$i = 0;
do {
$bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
$i += 2;
} while ($i < strlen($str));
return $bin;
}
Xampp is telling me theres something wrong on the last line (174) which would be the }, but there isn't a problem with it at all
bobo
-
-
You don't index into arrays with curly braces "{}" to index arrays in PHP, you use square ones [].
i.e:
Code:
$bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
Should actually be
Code:
$bin .= chr(hexdec($str[$i].$str[$i + 1]));
However this code isn't going to do what the function name implies (convert a hexadecimal string into a binary string).
-
I know, It's not my original code, i'm setting up a server, actually and it was one of the errors it was recieving.
-
Quote:
Originally Posted by
Jason
You don't index into arrays with curly braces "{}" to index arrays in PHP, you use square ones [].
i.e:
Code:
$bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
Should actually be
Code:
$bin .= chr(hexdec($str[$i].$str[$i + 1]));
However this code isn't going to do what the function name implies (convert a hexadecimal string into a binary string).
It doesn't matter. PHP is weird, but still a nice language. Curly braces (a.k.a. curly brackets: { }) as well as braces (a.k.a. brackets: [ ]) are just fine. People tend to use brackets because that looks better.
@Cyaphex Do you still need help? Cause I might have the solution for you. You should clearify your question how ever.
Edit:
Code:
<?php
$convert = '01234567890abcdef';
$convertFrom = 16; //Hexadecimal
$convertTo = 2; //Binary
echo base_convert($hex, $converFrom, $convertTo);
?>
Both $convertFrom and $convertTo have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35. You probably don't want to change this, since you only want a hex2bin. If you want bin2hex, just swap the variables.