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

  • What character set are you using? I think that will make a difference. http://php.net/manual/en/function.ord.php#103277 – jbchurchill Oct 02 '15 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. – Flosculus Oct 02 '15 at 16:23

1 Answers1

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.

Gabriel Rodriguez
  • 1,163
  • 10
  • 23
  • 2
    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 Oct 02 '15 at 16:30
  • 1
    @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. – domsson Apr 18 '17 at 21:16