PHP ord() function on extended ASCII table

2019-05-11 14:19发布

I'm facing a weird problem with the PHP ord() function when using it on ASCII characters from the extended ASCII table :

<?php
    echo ord('!'); // prints 33 : OK
    echo ord('a'); // prints 97 : OK

    echo ord('é'); // prints 195 : NOT OK
    echo ord('ü'); // prints 195 : NOT OK
?>

Do you have any idea why this function has this behavior ?

Moreover i'd like to specify that i'm actually trying to create a really simple steganography program that converts a message character by character into it's binary representation (using the ASCII table) and then creating a simple black and white 8xC pixels image (where C corresponds to the number of characters in total) and that this is why i'm dealing with this kind of stuff.

I was just wondering how to use and how to apply what I just learned about PHP GD's library, so i thought this would be a great training !

Thanks in advance for your answers (and sorry for my english) !

标签: php ord
1条回答
做个烂人
2楼-- · 2019-05-11 14:55

ord() does not supports utf-8. Use this function for unicode characters:

function uniord($u) { 
    $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8'); 
    $k1 = ord(substr($k, 0, 1)); 
    $k2 = ord(substr($k, 1, 1)); 
    return $k2 * 256 + $k1; 
} 

    echo ord('!'); // prints 33 : OK
    echo ord('a'); // prints 97 : OK

    echo uniord('é'); //233
    echo uniord('ü'); //252

PS: you can use uniord() for both type of characters perfectly.

查看更多
登录 后发表回答