-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. 如何編碼和解碼非ASCII字符?
- 6. 的Javascript Base64編碼
- 7. javascript unicode base64編碼
- 8. Markdown,Perl和字符編碼:呈現非ASCII字符
- 9. Base64在Java中的Ascii編碼
- 10. Rails 3編碼非ascii?
- 11. 爲什麼US-ASCII編碼接受非US-ASCII字符?
- 12. linux ascii到utf-16(然後sha1和base64)編碼
- 13. 編碼URI(空格等),不編碼非ASCII字符
- 14. ASCII編碼和UNICODE編碼
- 15. C#解碼非ASCII字符?
- 16. 將ascii字符串轉換爲不帶「b」和引號的base64
- 17. PyQt4字符編碼:'ascii'編解碼器不能編碼字符
- 18. PHP/Javascript/JQuery - base64 sha256編碼
- 19. Base64和utf8 /國家字符編碼
- 20. 需要幫助的ASCII和BASE64的UTF-8編碼使用字符串
- 21. Base64編碼和解碼
- 22. Android:編碼和解碼base64
- 23. Base64編碼和解碼
- 24. 使用Python熊貓時編碼/解碼非ASCII字符
- 25. 不使用「+/=」(加號或等號)字符的base64編碼?
- 26. 從Base64編碼字符串
- 27. base64編碼空終止符
- 28. MIME Base64編碼寬字符
- 29. java字符串base64編碼
- 30. 通過Flex JSON逃避非ASCII符號
問題是什麼? – 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