473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

binary string length

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
5 28369
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
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
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
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
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johannes | last post by:
Hi, I tried to pack eight integer values and one string into one binary string, which I would like to store in a mysql db. I encountered two problems doing this: 1) $this->packed =...
7
by: Dan Jones | last post by:
Hello, I'm trying to figure out how to get bit operators to work on a binary string. This is what I'm trying to do: list(frame) #where frame is a binary string y = frame << 8 It gives me a...
5
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int...
6
by: Tarun | last post by:
Hi All, I need to find a particular substring in a binary string(some text appended & prepended to binary data). I cant use strstr since it terminates on receiving '\0'which can be there in...
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
2
by: Bammer22 | last post by:
I am trying to BinaryWrite an image from a binary string that I am storing in the web.config file. The image is just a 1x1 pixel gif. I am setting the content type to "image/gif", but the output is...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
5
by: andrew.douglas11 | last post by:
Hello, I'm looking to display an image in the browser using a binary string containing all the bytes that make up a GIF image. I've tried all the standard encodings in System.Text.Encoding,...
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.