Home > Web Front-end > JS Tutorial > body text

JavaScript implements UTF-8 encoding and decoding

coldplay.xixi
Release: 2021-01-22 17:40:57
forward
5582 people have browsed it

JavaScript implements UTF-8 encoding and decoding

Free learning recommendation: javascript video tutorial

首先简单介绍一下UTF-8。UTF-8以字节为单位对Unicode进行编码。
UTF-8的特点是对不同范围的字符使用不同长度的编码。对于0x00-0x7F之间的字符,UTF-8编码与ASCII编码完全相同。
UTF-8编码的最大长度是6个字节。6字节模板有31个x,即可以容纳31位二进制数字。
Unicode的最大码位0x7FFFFFFF也只有31位。
Copy after login

From Unicode to UTF-8 The encoding method is as follows:

##000080-0007FF110xxxxx 10xxxxxx000800-00FFFF1110xxxx 10xxxxxx 10xxxxxx##010000-10FFFF##11110xxx10xxxxxx10xxxxxx10xxxxxxThe following is the js implementation code, first is the encoding
function utf8Encode(inputStr) {
  var outputStr = "";
  
  for(var i = 0; i < inputStr.length; i++) {
    var temp = inputStr.charCodeAt(i);
    
    //0xxxxxxx
    if(temp < 128) {
      outputStr += String.fromCharCode(temp);
    }
    //110xxxxx 10xxxxxx
    else if(temp < 2048) {
      outputStr += String.fromCharCode((temp >> 6) | 192);
      outputStr += String.fromCharCode((temp & 63) | 128);
    }
    //1110xxxx 10xxxxxx 10xxxxxx
    else if(temp < 65536) {
      outputStr += String.fromCharCode((temp >> 12) | 224);
      outputStr += String.fromCharCode(((temp >> 6) & 63) | 128);
      outputStr += String.fromCharCode((temp & 63) | 128);
    }
    //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    else {
      outputStr += String.fromCharCode((temp >> 18) | 240);
      outputStr += String.fromCharCode(((temp >> 12) & 63) | 128);
      outputStr += String.fromCharCode(((temp >> 6) & 63) | 128);
      outputStr += String.fromCharCode((temp & 63) | 128);
    }
  }
  
  return outputStr;
}
Copy after login
Unicode encoding (hexadecimal) UTF-8 byte stream (binary)
000000-00007F 0xxxxxxx
The following is the decoding

function utf8Decode(inputStr) {
  var outputStr = "";
  var code1, code2, code3, code4;
  
  for(var i = 0; i < inputStr.length; i++) {
    code1 = inputStr.charCodeAt(i);
    
    if(code1 < 128) {
      outputStr += String.fromCharCode(code1);
    }
    else if(code1 < 224) {
      code2 = inputStr.charCodeAt(++i);
      outputStr += String.fromCharCode(((code1 & 31) << 6) | (code2 & 63));
    }
    else if(code1 < 240) {
      code2 = inputStr.charCodeAt(++i);
      code3 = inputStr.charCodeAt(++i);
      outputStr += String.fromCharCode(((code1 & 15) << 12) | ((code2 & 63) << 6) | (code3 & 63));
    }
    else {
      code2 = inputStr.charCodeAt(++i);
      code3 = inputStr.charCodeAt(++i);
      code4 = inputStr.charCodeAt(++i);
      outputStr += String.fromCharCode(((code1 & 7) << 18) | ((code2 & 63) << 12) |((code3 & 63) << 6) | (code2 & 63));
    }
  }
  
  return outputStr;
}
Copy after login
and above!

Related free learning recommendations:

javascript(Video)

The above is the detailed content of JavaScript implements UTF-8 encoding and decoding. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!