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.

Found on the interweb: http://www.inter-locale.com/demos/countBytes.html


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

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.