1

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"

cusspvz
  • 5,143
  • 7
  • 30
  • 45

2 Answers2

2

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.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • i can't, it must be with jQuery, all my page is on ajax, and all data is processed by client side. If i do it server side, would be a little nasty -.- – cusspvz Jul 12 '10 at 10:42
  • What? No, sending SMS from the webpage would be "nasty" as you put it. Use jQuery to submit the textbox **as text** to the server, and have the server send the SMS. Seriously, don't do it from the client. – Neil Barnwell Jul 12 '10 at 10:44
  • The SMS text isn't the only thing it sends, it sends the client numbers, etc. the response will create images to dettermine if the sms was sent or not. If the sms has too many chars what do i tell to user? if i put that on server side i need to create another event to call alerts from server on the page... – cusspvz Jul 12 '10 at 10:46
  • Presumably you're going to be using an authenticated service, which will require credentials or authentication token to be provided in the request. Including this in the JS of the page is going to be a serious problem. You really need to do at least some of the process server-side to alleviate these issues. – ptomli Jul 12 '10 at 10:47
  • the authentication is made before the script load. If the user isn't authenticated, he will not load the script. i will edit my question... – cusspvz Jul 12 '10 at 10:50
0

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

ptomli
  • 11,730
  • 4
  • 40
  • 68
  • the http request will be sent to my servers, they will be evaluate the client, if he has bills to pay etc, and then if the sms has validated, will send the sms through our api to bulksms servers ;) (i'm your client) – cusspvz Jul 12 '10 at 10:44
  • Then I suggest your course of action is to filter characters which cannot be sent via standard 7bit messages. You can do this client-side, to help the UI, but you really should also do it server-side. Any characters in the message body which are part of the extended GSM charset will need to be escaped, as per the docs. Otherwise you'll have to send unicode messages, but that means a bit more complication, and reduced message length. – ptomli Jul 12 '10 at 10:54
  • can you give me your email contact? does your EAPI have that function? i've edited my question. thanks – cusspvz Jul 12 '10 at 10:56
  • Send an email to support at bulksms dot com, request it be escalated FAO pault – ptomli Jul 12 '10 at 10:59
  • Hi again pault, i've already sent an email... can you respond it please? this issue is urgent... very thank you – cusspvz Jul 12 '10 at 11:40