3

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) !

2
  • What character set are you using? I think that will make a difference. php.net/manual/en/function.ord.php#103277 Commented Oct 2, 2015 at 16:12
  • Only 7-bit ASCII characters use single bytes in UTF-8, the remaining values are used to determine the length of the multi-byte character. So your file encoding will need to be ASCII to read those characters as single bytes. Commented Oct 2, 2015 at 16:23

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

To elaborate, the reason é showed ASCII 195 is because it's actually a two-byte character (UTF-8), the first byte of which is ASCII 195.
@Mr.Llama your comment is actually more of an answer than this answer, as OP specifically asked why ord() behaves the way it does, not how he can work around its behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.