简体   繁体   中英

reuse the value of str_split()

I have a long string of number , I want to divide them into chunks of length 8 then get their characters out of this ascii number.

when I use bindec() after str_split($string,8) it gives me wrong value. I can not figure out where is the problem as str_split() divides the string correctly.

$key = '101101011';
numbersToletters($key);

function numbersToletters($string) {
    $pool = str_split($string, 8);
    for($i = 0; $i < count($pool); $i++) {
        $bin = $pool[$i];
        $n = bindec($bin);
        echo chr($n) . "<br>";
    }
}

the output I receive out of bindec('101101011') is 81 while it is 363 and should return k

this works fin for example

$number = bindec($key);
echo $number;
echo "<br>".chr($number) ."<br>";

Remove the call to chr function:

$key = '0011010000101101101101011010100000011101110110111001000000000100';
numbersToletters($key);

function numbersToletters($string) {
    $pool = str_split($string, 8);
    for($i = 0; $i < count($pool); $i++) {
        $bin = $pool[$i];
        $n = bindec($bin);
        echo $n . "<br>";
    }
}

I try your code and nothing is wrong. Var_dump show true values and I translated with my head to be sure.

array (size=8)
0 => string '00110100' (length=8)
1 => string '00101101' (length=8)
2 => string '10110101' (length=8)
3 => string '10101000' (length=8)
4 => string '00011101' (length=8)
5 => string '11011011' (length=8)
6 => string '10010000' (length=8)
7 => string '00000100' (length=8)

52
45
181
168
29
219
144
4

Check on http://www.asciitable.com/

    $key = '0011010000101101101101011010100000011101110110111001000000000100';
    $pool = str_split($key,8);

    var_dump($pool);

    for($i = 0 ;$i < count($pool) ; $i++)
    {
        $n= bindec($pool[$i]);
        var_dump($n);
        echo chr($n) . "<br>";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM