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"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.
What system are you using to send the SMS from jQuery? I suspect it will be via a HTTP request, in which case the service should do encoding for you. In some cases there are characters which are not represented in the GSM character set, and other cases where characters need to be represented as an 'escaped pair' of bytes.
The documentation for the service you're using should be able to inform you of the correct formatting for the SMS body.
For example: http://www.bulksms.com/int/docs/eapi/submission/character_encoding/
Full disclosure: I work for BulkSMS
精彩评论