-4
A
回答
4
如果你使用Unicode你最后用Base64编码字符串中的工作,我会建议使用由WebToolkit.info提出以下脚本。 脚本与UTF-8编码完全兼容。
/**
*
* Base64 encode/decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
0
没有语言默认解决方案来获取字符串为base64。你必须写自己的功能,或者偷这一个:
http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html
相关问题
- 1. Base64编码:非法base64字符3c
- 2. Javascript Ascii编码
- 3. 将非ASCII字符编码为UTF-16
- 4. 在C#.NET中编码非ascii字符
- 5. 的Javascript Base64编码
- 6. Base64在Java中的Ascii编码
- 7. Rails 3编码非ascii?
- 8. 为什么US-ASCII编码接受非US-ASCII字符?
- 9. linux ascii到utf-16(然后sha1和base64)编码
- 10. C#解码非ASCII字符?
- 11. 编码URI(空格等),不编码非ASCII字符
- 12. ASCII编码和UNICODE编码
- 13. 将ascii字符串转换为不带“b”和引号的base64
- 14. PyQt4字符编码:'ascii'编解码器不能编码字符
- 15. Android:编码和解码base64
- 16. Base64编码和解码
- 17. 不使用“+/=”(加号或等号)字符的base64编码?
- 18. MIME Base64编码宽字符
- 19. base64编码空终止符
- 20. 从Base64编码字符串
- 21. java字符串base64编码
- 22. Base64编码为base64,
- 23. 标题在JavaScript中用于变音符号的情况(非ASCII)
- 24. 使用javascript编辑base64编码图像
- 25. Base64编码
- 26. 非ascii文件名的编码
- 27. 反转包含ASCII字符和非ASCII字符的字符串
- 28. 反向base64编码和gzcompress
- 29. MD5哈希和Base64编码
- 30. Java Base64编码的字符串与.NET Base64编码的字符串
问题是什么? – 2013-03-10 11:38:08
Base64编码的字符串*不能包含非ASCII字符。 – VisioN 2013-03-10 11:38:24
你的意思是输入字符串包含非ASCII?您应该可以对UTF-8编码的非ASCII符号进行64位编码。 – Rup 2013-03-10 11:39:37