一、js計算字符串的字節數方法:
//blob獲取字符串的字節 var debug = "好的"; var blob = new Blob([debug],{type : 'text/plain'}); console.log(blob) /** * 計算字符串所占的內存字節數,默認使用UTF-8的編碼方式計算,也可制定為UTF-16 * UTF-8 是一種可變長度的 Unicode 編碼格式,使用一至四個字節為每個字符編碼 * * 000000 - 00007F(128個代碼) 0zzzzzzz(00-7F) 一個字節 * 000080 - 0007FF(1920個代碼) 110yyyyy(C0-DF) 10zzzzzz(80-BF) 兩個字節 * 000800 - 00D7FF 00E000 - 00FFFF(61440個代碼) 1110xxxx(E0-EF) 10yyyyyy 10zzzzzz 三個字節 * 010000 - 10FFFF(1048576個代碼) 11110www(F0-F7) 10xxxxxx 10yyyyyy 10zzzzzz 四個字節 * * 注: Unicode在范圍 D800-DFFF 中不存在任何字符 * {@link http://zh.wikipedia.org/wiki/UTF-8} * * UTF-16 大部分使用兩個字節編碼,編碼超出 65535 的使用四個字節 * 000000 - 00FFFF 兩個字節 * 010000 - 10FFFF 四個字節 * * {@link http://zh.wikipedia.org/wiki/UTF-16} * @param {String} str * @param {String} charset utf-8, utf-16 * @return {Number} */ var sizeof = function(str, charset){ var total = 0, charCode, i, len; charset = charset ? charset.toLowerCase() : ''; if(charset === 'utf-16' || charset === 'utf16'){ for(i = 0, len = str.length; i < len; i++){ charCode = str.charCodeAt(i); if(charCode <= 0xffff){ total += 2; }else{ total += 4; } } }else{ for(i = 0, len = str.length; i < len; i++){ charCode = str.charCodeAt(i); if(charCode <= 0x007f) { total += 1; }else if(charCode <= 0x07ff){ total += 2; }else if(charCode <= 0xffff){ total += 3; }else{ total += 4; } } } return total; } console.log(sizeof(debug))
二、字符串與二進制的相互轉化:
//字符串轉ascii碼,用charCodeAt(); //ascii碼轉字符串,用fromCharCode(); var str = "A"; var code = str.charCodeAt(); var str2 = String.fromCharCode(code);
十進制轉二進制 var a = "i"; console.log(a.charCodeAt()); //105 console.log(a.charCodeAt().toString(2)); //1101001
//將字符串轉換成二進制形式,中間用空格隔開 function strToBinary(str){ var result = []; var list = str.split(""); for(var i=0;i<list.length;i++){ if(i != 0){ result.push(" "); } var item = list[i]; var binaryStr = item.charCodeAt().toString(2); result.push(binaryStr); } return result.join(""); } console.log(strToBinary("我們二進制數據y_+=ha好的")); //110001000010001 100111011101100 100111010001100 1000111111011011 101001000110110 110010101110000 110001101101110 1111001 1011111 101011 111101 1101000 1100001 101100101111101 111011010000100 console.log(strToBinary("@%$+測試{'obj':'124'}")); //1000000 100101 100100 101011 110110101001011 1000101111010101 1111011 100111 1101111 1100010 1101010 100111 111010 100111 110001 110010 110100 100111 1111101 //將二進制字符串轉換成Unicode字符串 function binaryToStr(str){ var result = []; var list = str.split(" "); for(var i=0;i<list.length;i++){ var item = list[i]; var asciiCode = parseInt(item,2); var charValue = String.fromCharCode(asciiCode); result.push(charValue); } return result.join(""); } console.log(binaryToStr("110001000010001 100111011101100 100111010001100 1000111111011011 101001000110110 110010101110000 110001101101110 1111001 1011111 101011 111101 1101000 1100001 101100101111101 111011010000100")); //我們二進制數據y_+=ha好的 console.log(binaryToStr("1000000 100101 100100 101011 110110101001011 1000101111010101 1111011 100111 1101111 1100010 1101010 100111 111010 100111 110001 110010 110100 100111 1111101")); //@%$+測試{'obj':'124'}