By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

binary string length

P: n/a
Hi

strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()

thks.

May 28 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
gezerpunta wrote:
strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()
Then use filesize(). D'oh!

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
May 28 '07 #2

P: n/a
On May 28, 10:40 am, gezerpunta <css...@gmail.comwrote:
Hi

strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()

thks.
http://us.php.net/manual/en/function...rlen.php#72979

Google searches ("php binary string length") come in handy...

May 28 '07 #3

P: n/a
On May 28, 6:02 pm, "farri...@gmail.com" <farri...@gmail.comwrote:
On May 28, 10:40 am, gezerpunta <css...@gmail.comwrote:
Hi
strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()
thks.

http://us.php.net/manual/en/function...rlen.php#72979

Google searches ("php binary string length") come in handy...
In PHP, like in C, the string ends with a zero-character, '\0', (char)
0, null-terminator, null-byte or whatever you like to call it. Thus,
if you have binary strings, they probably have this character
somewhere inside them, although that doesn't mean it's the end of the
string. From this reason, it's not very smart to use ordinary
strlen(), and str* functions in general, for binary data. Use, as
farrishj suggested, mb-strlen (multibyte string), or use filesize()
directly, as Ortega suggested. You can, however, make your own
functions to take care of this, just remember the number of bytes you
read together with the data - this, of course, if aforementioned
functions are not good enough for you.

May 28 '07 #4

P: n/a
On 28 May 2007 09:29:46 -0700, Darko <da**************@gmail.comwrote:
>On May 28, 6:02 pm, "farri...@gmail.com" <farri...@gmail.comwrote:
>On May 28, 10:40 am, gezerpunta <css...@gmail.comwrote:
strlen does not return the correct value .I compared the filesize()
and strlen byte size but they are not equal. I must find binary string
length and it must be equal to filesize()

http://us.php.net/manual/en/function...rlen.php#72979

Google searches ("php binary string length") come in handy...

In PHP, like in C, the string ends with a zero-character, '\0', (char)
0, null-terminator, null-byte or whatever you like to call it.
No, that's not the case - PHP strings are stored with both the length and the
data, unlike C strings that just has one pointer and uses a terminator. They're
"binary-safe" - NUL doesn't terminate the string.

See the definition of zvalue_value in zend.h; the string part has both a "char
*val" and "int len".

Problems would start if you're using the mbstring.func_overload, which changes
how strlen() and the other functions work, and does try and treat strings as
strings of characters in a specific encoding rather than a string of bytes.
This is not the normal PHP behaviour.

$ cat test.php
<?php
$x = chr(0) . chr(0) . "blah" . chr(0) . chr(0);
print strlen($x);
?>

$ php test.php
8

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
May 28 '07 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andy Hassall wrote:
No, that's not the case - PHP strings are stored with both the length and the
data, unlike C strings that just has one pointer and uses a terminator. They're
"binary-safe" - NUL doesn't terminate the string. [snip]
Andy is correct. By default, PHP strings are treated like binary
strings, which works great for 8-bit encodings but not so much for UTF-8
or other multibyte character encodings. mbstring tries to "fix" this
using the automatic string overload, but then you lose the ability to
process binary data.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGWytBqTO+fYacSNoRAgcBAJ9FrhQ62Z6HVRFBgOKwiX IiciAZjACfXhL4
A9uNj8G02gyQOOpp1Q/vVhs=
=xbaE
-----END PGP SIGNATURE-----
May 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.