binary string length

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gezerpunta

    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.

  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: binary string length

    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.

    Comment

    • farrishj@gmail.com

      #3
      Re: binary string length

      On May 28, 10:40 am, gezerpunta <css...@gmail.c omwrote:
      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.


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

      Comment

      • Darko

        #4
        Re: binary string length

        On May 28, 6:02 pm, "farri...@gmail .com" <farri...@gmail .comwrote:
        On May 28, 10:40 am, gezerpunta <css...@gmail.c omwrote:
        >
        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.
        >

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

        Comment

        • Andy Hassall

          #5
          Re: binary string length

          On 28 May 2007 09:29:46 -0700, Darko <darko.maksimov ic@gmail.comwro te:
          >On May 28, 6:02 pm, "farri...@gmail .com" <farri...@gmail .comwrote:
          >On May 28, 10:40 am, gezerpunta <css...@gmail.c omwrote:
          >>
          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_o verload, 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 :: andy@andyh.co.u k :: http://www.andyh.co.uk
          http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

          Comment

          • Edward Z. Yang

            #6
            Re: binary string length

            -----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.o rg 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 +fYacSNoRAgcBAJ 9FrhQ62Z6HVRFBg OKwiXIiciAZjACf XhL4
            A9uNj8G02gyQOOp p1Q/vVhs=
            =xbaE
            -----END PGP SIGNATURE-----

            Comment

            Working...