SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    USA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Get String Length in Bytes

    Is there some way to get a string's length in bytes? I need to support UTF-8 inputs, so a character could range from one to four bytes. Or is the only way to find the bytes is to encode each character and count the number of bytes it takes?

    Thanks.

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found on the interweb: http://www.inter-locale.com/demos/countBytes.html
    Code:
      <script type="text/javascript">
         function checkLength() {
            var countMe = document.getElementById("someText").value
            var escapedStr = encodeURI(countMe)
            if (escapedStr.indexOf("&#37;") != -1) {
                var count = escapedStr.split("%").length - 1
                if (count == 0) count++  //perverse case; can't happen with real UTF-8
                var tmp = escapedStr.length - (count * 3)
                count = count + tmp
            } else {
                count = escapedStr.length
            }
            alert(escapedStr + ": size is " + count)
         }
      </script>
    var me = null;

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you need to know that?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    USA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Why would you need to know that?
    I need to know that Because the form data is being passed to a C program and if the user submits something in Chinese, it overruns the character array and corrupts memory. I didn't want to shrink the text field to maxlength/4 or do a strncpy in the C code that would corrupt the text.


    Thank you for the script, jtrelfa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •