ord() on binary strings

php.internals

Derick Rethans

20 years ago
Hei, I committed a patch to make ord() work on IS_BINARY strings earlier today (http://news.php.net/php.cvs/33862). Andrei doesn't like that because the description in the docs says "ord -- Return ASCII value of character", which in his opinion doesn't make much sense for binary strings. Other then ord(), there is no suitable function to show the ordinal value of a byte in a IS_BINARY string though. Comments? Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Ilia A.

20 years ago
It makes perfect sense to me that ord() would work on binary strings. When we return an ASCII value we don't limit the range returned, it can be 1 or 255. So, I see no reason to cripple this function and prevent it from working on binary data. Ilia

Derick Rethans

20 years ago
On Thu, 8 Sep 2005, Ilia Alshanetsky wrote:
> It makes perfect sense to me that ord() would work on binary strings. > When we return an ASCII value we don't limit the range returned, it can > be 1 or 255. So, I see no reason to cripple this function and prevent it > from working on binary data.
Indeed, the only thing is that you can't really talk about "ASCII" values, as binary data doesn't really have an encoding (and certainly not ascii as that's 7bits only). But IMO that's something to tune in the docs. Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Andi Gutmans

20 years ago
Did Andrei have a reason besides the docs? Even without IS_UNICODE/IS_BINARY it sounds like the docs could use tuning anyway, no? Andi At 12:55 PM 9/8/2005, Derick Rethans wrote:

Derick Rethans

20 years ago
On Thu, 8 Sep 2005, Andi Gutmans wrote:
> Did Andrei have a reason besides the docs? Even without IS_UNICODE/IS_BINARY > it sounds like the docs could use tuning anyway, no?
Andrei: The docs say "Return ASCII value of character", which doesn't make sense for binary. You can use bin2hex(), bin2hex() == ord() for binary. (Which is not true, as it doesn't return the ordinal value, just a two digit hex string) Derick: anyway, ord() always worked on string like this, seems strange to break it (and yes, it already did) Andrei: you had binary strings before? Derick: normal strings of course, but it came from a file, so it turned binary and ord() started failing Andrei: Then you should fix your code and convert that binary to something sensible if you are reading text Derick: it's not text, it's binary Andrei: if it's binary, why do you need to know the ascii value Derick: Because I'm converting a binary file to a C file (for the timezone database) Then he continues saying that it's Ok to break BC in unicode mode. regards, Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Andrei Zmievski

20 years ago
My argument was that ord() semantics and docs say that it works on _characters_ and this simply does not make sense for the binary strings. If we really want ord() to work on binary type, fine, but we'd better have docs that make sense then. -Andrei On Sep 9, 2005, at 12:09 AM, Derick Rethans wrote:

Ilia A.

20 years ago
Andrei Zmievski wrote:
> My argument was that ord() semantics and docs say that it works on > _characters_ and this simply does not make sense for the binary > strings. If we really want ord() to work on binary type, fine, but we'd > better have docs that make sense then.
IMHO the docs say characters because up until now, a character was equivalent to a single byte in PHP. I think this is a documentation snafu, that should be addressed by updating the docs accordingly. Ilia

Dmitry Stogov

20 years ago
I don't see any problems with ord() for binary data. String consist from characters and ord() returns the number of character. Binary string consist from bytes and ord() returns value of byte. Dmitry.

Tex Texin

20 years ago
I am with Andrei on this. Ord doesn't make strict sense, but may also be confusing when all of its functionality is taken into account. As a function that returns an int representing the first byte of a string of bytes it has limited value. Indexing that byte or using substring would be more generic and accomplish pretty much the same. There is some benefit in having ord() be available to binary for similarity, but it can also hurt you because someone might think that if their byte data represents utf-16, that ord will return the first 16 bit char. Further, (I forget) does ord do a conversion from the unicode string encoding to the runtime encoding? Wouldn't some conversion be implied for binary data then also? ugh. Tex Texin Internationalization Architect, Yahoo! Inc.