jQuery SMS字符计算器,具有7位和16位 [英] jquery SMS character calculator with 7-bit and 16bit

查看:98
本文介绍了jQuery SMS字符计算器,具有7位和16位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段,可以用英文和中文键入短信.正如我搜索过的,1个短信只能有1120位.每个英文字符为7位,因此可以为1120/7 = 160个字符;对于中文,每个字符为16位,因此为1120/16 = 70个字符.我需要使用jquery来显示文字字段和文本字段下剩余的单词.我该怎么做呢?

I have a text field to type sms message both in english and chinese language. As I have searched, 1 sms can only have 1120 bits. Each english character is 7 bit so it can be 1120/7 = 160 characters and for chinese each character is 16 bits so that is 1120/16 = 70 characters. I need to use jquery to show the words written and words remaining under the text field. how do i do this?

推荐答案

字符可以是单字节,双字节,三字节等.所以单字节遵循一个特定的范围.其他字符也是如此.基于此,我创建了以下函数,这些函数将根据内存来计算字符串的大小

Characters can be single byte ,double byte, triple byte and so on. So single byte follows in a particular range.Same thing is true for other characters.Based on this I have created following functions that will calculate the size of a string on the basis of memory

function getByteLength(normal_val) {
    // Force string type
    normal_val = String(normal_val);

    var byteLen = 0;
    for (var i = 0; i < normal_val.length; i++) {
        var c = normal_val.charCodeAt(i);
        byteLen +=  c < (1 <<  7) ? 1 :
                    c < (1 << 11) ? 2 :
                    c < (1 << 16) ? 3 :
                    c < (1 << 21) ? 4 :
                    c < (1 << 26) ? 5 :
                    c < (1 << 31) ? 6 : Number.NaN;
    }
    return parseInt(byteLen)*8;
} 

我创建了一个js小提琴,将为您工作. http://jsfiddle.net/paraselixir/d83oaa3v/6/

I have created a js fiddle that will work for you. http://jsfiddle.net/paraselixir/d83oaa3v/6/

这篇关于jQuery SMS字符计算器,具有7位和16位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆