# ord() on binary strings

- Thread: https://externals.io/message/18723
- Original archive: https://news-web.php.net/php.internals/18723
- Started by Derick Rethans on 2005-09-08T19:55:04+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #18723 — Derick Rethans — 2005-09-08T19:55:04+00:00

URL: https://externals.io/message/18723#18723

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

---

## #18728 — Ilia Alshanetsky — 2005-09-08T21:20:43+00:00

URL: https://externals.io/message/18723#18728 · In reply to: #18723

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

---

## #18733 — Andi Gutmans — 2005-09-08T22:34:23+00:00

URL: https://externals.io/message/18723#18733 · In reply to: #18723

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:
>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

---

## #18734 — Derick Rethans — 2005-09-09T07:02:14+00:00

URL: https://externals.io/message/18723#18734 · In reply to: #18728

> 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

---

## #18735 — Derick Rethans — 2005-09-09T07:09:09+00:00

URL: https://externals.io/message/18723#18735 · In reply to: #18733

> 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

---

## #18741 — Andrei Zmievski — 2005-09-09T13:41:35+00:00

URL: https://externals.io/message/18723#18741 · In reply to: #18735

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




> 
>
>
> > […]
>
> 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

---

## #18743 — Ilia Alshanetsky — 2005-09-09T13:46:07+00:00

URL: https://externals.io/message/18723#18743 · In reply to: #18741

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
