International sms character counter

I found the number of characters / sms using jQuery , but it does not support international characters like Chinese, Japanese, Thai, etc.

var $remaining = $('#remaining'), $messages = $remaining.next(); $('#message').keyup(function(){ var chars = this.value.length, messages = Math.ceil(chars / 160), remaining = messages * 160 - (chars % (messages * 160) || messages * 160); $remaining.text(remaining + ' characters remaining'); $messages.text(messages + ' message(s)'); }); 

Here are some examples of the wrong number of characters:

您好, 請問 你 吃飯 了 嗎 <<11 characters

สวัสดี คุณ กิน หรือ? & L; <17 characters

こ ん に ち は, あ な た は 食 べ て い ま す か か <<18 characters

안녕하세요, 당신 이 먹는 거죠? & L; <17 characters

हैलो, आप खाते ह ैं? & L; <18 characters

Good Zen, are you a lamb? & L; <22 characters

How can I do this work with non-ASCII characters?

+4
source share
1 answer

You cannot count on "characters" here. According to the SMS article on Wikipedia, SMS uses one of three different encodings (7-bit GSM, 8-bit GSM and UTF-16). So first you need to know / decide which encoding you will use.

If you know that you will always use UTF-16, then you can calculate the number of 16-bit code units that the string will be in. Standard SMS can consist of 70 16-bit code blocks. But this will limit the messages in Latin letters to 70. Therefore, if you want to use the full 160 characters (with 7-bit encoding) or 140 characters (with 8-bit encoding) for Latin characters, you will need to distinguish between three cases.

Example for counting 16-bit UTF-16 code blocks:

 var message = "您好,請問你吃飯了嗎?"; var utf16codeUnits = 0; for (var i = 0, len = message.length; i < len; i++) { utf16codeUnits += message.charCodeAt(i) < 0x10000 ? 1 : 2; } 

By the way, this one will come up with the same amount as you as “wrong”, so you will need to explain why you think they are wrong.


EDIT

Despite the fact that I have already been accepted, I quickly hacked a function that correctly (as far as I can tell) calculates 7-bit (if possible) GSM signals and UTF-16 SMS messages: http://jsfiddle.net/puKJb /

+9
source

Source: https://habr.com/ru/post/1345496/


All Articles