Search code examples
javascriptjquerysmsbytechars

jQuery - How to convert string from textbox to bytes?


i need to convert a string from a text field to bytes. How can i do that?

Reason: The text on the textbox will be sent by SMS to the client, and I've limited the text to 160chars but if i put special chars like @€£‰¶÷‰‰€£@ it will be larger than 256 bytes (that's the size of an sms).

So i need to remove from 256 the converted text to bytes, and convert the result to normal chars length.

Example:

var charsleft = toChars(256 - toBytes(mystring));

EDIT:
REASON to be on Javascript: i have an line saying something like this "XXX chars left"


Solution

  • Do it serverside with a language you prefer. Java/ECMA script is really a bad place for such operations. It is already a mess with character encoding so do yourself a favour and skip the hassle with content-types and stuff. Send the string as UTF8 (for instance) to your server and do the work there.