javascriptjqueryhtmlbit-packing

jquery SMS character calculator with 7-bit and 16bit


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?


Solution

  • 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;
    } 
    

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