JS implementation of the Chinese string utf-8 Base64 encoding method (make it the same as Java coding) _javascript skills

Source: Internet
Author: User
Tags base64


In this paper, we describe the method of Base64 encoding for utf-8 of Chinese strings by JS. Share to everyone for your reference, specific as follows:



String to encode: "Select username from user"



Using Java for encoding, Java programs:


String sql = "Select User name from user";
String encodestr = new String (Base64.encode (Sql.getbytes ("UTF-8")); Coding
System.out.println (ENCODESTR);


Get:


c2vszwn0ioeuqoait+wqjsbmcm9tioeuqoaitw==


Decoding in Java:


sql = new String (Base64.decode (Sql.getbytes ()), "UTF-8");


Why do you use GetBytes ("UTF-8") in Java code? Because the default encoding is different in Windows and Linux environments, you must specify the encoding for your program to get the same encoding on different platforms.



Although the HTML and JS encoding is utf-8, but JS from the page to get the Chinese code is utf-16, so directly to the Chinese Base64 encoding will get the wrong result, so we have to go from utf-16 to Utf-8 and then code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!
Body{
Margin:0px;
Padding:0px;
}
Body, td{
font-size:9pt;
}
>
</style>
<script type="text/JavaScript">
<!
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
//Base64 the ANSI encoded string
function encode64(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
Var I = 0;
Do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 &amp; 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 &amp; 15) << 2) | (chr3 >> 6);
enc4 = chr3 &amp; 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
Enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
+ keyStr.charAt(enc3) + keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
//Converting Base64 encoded strings to ANSI encoded strings
function decode64(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
Var I = 0;
if (input.length % 4 != 0) {
Return "";
}
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
Return "";
}
Do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 &amp; 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 &amp; 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output += String.fromCharCode(chr2);
}
if (enc4 != 64) {
output += String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
function utf16to8(str) {
var out, i, len, c;
Out = "";
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) &amp;&amp; (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) &amp; 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) &amp; 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) &amp; 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) &amp; 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) &amp; 0x3F));
}
}
Return out;
}
function utf8to16(str) {
var out, i, len, c;
var char2, char3;
Out = "";
len = str.length;
I = 0;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
/ / 0xxxxxxx
out += str.charAt(i-1);
Break;
case 12: case 13:
// 110x xxxx  10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c &amp; 0x1F) << 6) | (char2 &amp; 0x3F));
Break;
Case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c &amp; 0x0F) << 12) |
((char2 &amp; 0x3F) << 6) |
((char3 &amp; 0x3F) << 0));
Break;
}
}
Return out;
}
//Test code start
Var de = encode64 (utf16to8 ("select user name from user");
document.writeln(de+"<br>");
var ee = utf8to16(decode64(de))
document.writeln(ee);
//End of test code
/ / - >
</script>
</head>
<body>
</body>
</html> 


The above code is derived from the Internet, pieced together to get the correct results, thanks to the predecessors



PS: Here again for you to recommend several base64 encoding and decoding online tools, I believe that in future development will be used:



BASE64 Encoding and decoding tool:
http://tools.jb51.net/transcoding/base64



Online Image conversion BASE64 tool:
http://tools.jb51.net/transcoding/img2base64



More readers interested in JavaScript-related content can view the site topics: "JavaScript switching effects and tips summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and tips summary", " JavaScript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical calculation usage Summary



I hope this article will help you with JavaScript programming.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.