懂编码的进来看看
firefox吧
全部回复
仅看楼主
level 8
abc😁😂 楼主
想把单个字节编码成UTF-8,如何做呢?
比如有个byte为151,我用C#的Encoding.UTF8.GetString得到的字符的十进制值为65533,我想知道是怎么得出的。
2011年11月13日 12点11分 1
level 8
abc😁😂 楼主
凡是超过127的byte,全部为这个值,好像是对超过127的单个byte都认为是Unicode,但是不解如何得出的65533
2011年11月13日 13点11分 2
level 8
abc😁😂 楼主
计算机专业的少年,快点来
2011年11月13日 13点11分 3
level 13
银时[害羞]等7#
2011年11月13日 13点11分 6
level 9
[88]等8楼的,7楼的啥也不懂
2011年11月13日 14点11分 7
level 8
abc😁😂 楼主
我是用的js写的base64编解码,并且自己按照UTF8规则,写了UTF-8的编码函数。
如果按照编解码都按照UTF8来的话是没有问题,可是我突发奇想,也是为了检验我的UTF8有没有bug,于是就用C#带的方法,先用Unicode编码得到base64,然后用UTF8编码解码base64,事实证明,bug是没有,就是结果不一样。比较发现,C#里对字节值为0的不显示,对大于127的字节值全是65533这个值,也就是乱码。
下面是C#里的,"非base64编码"就是原字符串,XpdiAGEAcwBlADYANAAWfwF4就是用Unicode得到的base64字符串,再用UTF8解码得到“^�base64x”:
byte[] bytes = Encoding.Unicode.GetBytes("非base64编码");
Response.Write(Convert.ToBase64String(bytes)); Response.Write(Encoding.UTF8.GetString(Convert.FromBase64String("XpdiAGEAcwBlADYANAAWfwF4")));
2011年11月13日 14点11分 10
level 8
abc😁😂 楼主
是65535
2011年11月13日 14点11分 11
level 8
abc😁😂 楼主
<!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>
<script type="text/javascript">
var Base64 = function () { };
Base64.prototype = {
str: "",
EncodeChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
GetBytes: function () {
var _len = this.str.length;
var _bytes = [];
for (var i = 0; i < _len; i++) {
var _b = this.EncodingByUTF8(this.str.charCodeAt(i));
for (var j = 0; j < _b.length; j++) {
_bytes.push(_b[j]);
}
}
return _bytes;
},
EncodingByUTF8: function (_byte) {
if (_byte >= 0x0 && _byte <= 0x7F) {
return [_byte];
}
else if (_byte >= 0x80 && _byte <= 0x7FF) {
var b1 = (0x3 << 0x6) | (_byte >> 0x6);
var b2 = (0x1 << 0x7) | (_byte & 0x3F);
return [b1, b2];
}
else if (_byte >= 0x800 && _byte <= 0xFFFF) {
var b1 = (0x7 << 0x5) | (_byte >> 0xC);
var b2 = (0x1 << 0x7) | ((_byte >> 0x6) & 0x3F);
var b3 = (0x1 << 0x7) | (_byte & 0x3F);
return [b1, b2, b3];
}
else if (_byte >= 0x10000 && _byte <= 0x10FFFF) {
var b1 = (0xF << 0x4) | (_byte >> 0x12);
var b2 = (0x1 << 0x7) | ((_byte >> 0xC) & 0x3F);
var b3 = (0x1 << 0x7) | ((_byte >> 0x6) & 0x3F);
var b4 = (0x1 << 0x7) | (_byte & 0x3F);
return [b1, b2, b3, b4];
}
},
DecodingUTF8: function (_bytes) {
var _ch;
switch (_bytes.length) {
case 1:
if (_bytes[0] != 0) {//这里按照C#里的方式对0值不显示
_ch = String.fromCharCode(_bytes[0]);
}
break;
case 2:
_ch = String.fromCharCode(((_bytes[0] & 0x1F) << 0x6) | (_bytes[1] & 0x3F));
break;
case 3:
_ch = String.fromCharCode((((_bytes[0] & 0xF) << 0xC) | ((_bytes[1] & 0x3F) << 0x6)) | (_bytes[2] & 0x3F));
break;
case 4:
_ch = String.fromCharCode(((((_bytes[0] & 0x7) << 0x12) | ((_bytes[1] & 0x3F) << 0xC)) | ((_bytes[2] & 0x3F) << 0x6)) | (_bytes[3] & 0x3F));

2011年11月13日 14点11分 14
level 8
abc😁😂 楼主
break;
}
return _ch;
},
EncodingBase64: function () {
var _bytes = this.GetBytes();
var _len = _bytes.length;
var _mod = _len % 3;
var _dev = Math.floor(_len / 3);
var _result = [];
for (var i = 0; i < _dev; i++) {
_result.push(this.EncodeChars.charAt(_bytes[i * 3] >> 0x2));
_result.push(this.EncodeChars.charAt(((_bytes[i * 3] & 0x3) << 0x4) | (_bytes[i * 3 + 1] >> 0x4)));
_result.push(this.EncodeChars.charAt(((_bytes[i * 3 + 1] & 0xF) << 0x2) | (_bytes[i * 3 + 2] >> 0x6)));
_result.push(this.EncodeChars.charAt(_bytes[i * 3 + 2] & 0x3F));
}
switch (_mod) {
case 1:
_result.push(this.EncodeChars.charAt(_bytes[_bytes.length - 1] >> 0x2));
_result.push(this.EncodeChars.charAt((_bytes[_bytes.length - 1] & 0x3) << 0x4));
_result.push("==");
break;
case 2:
_result.push(this.EncodeChars.charAt(_bytes[_bytes.length - 2] >> 0x2));
_result.push(this.EncodeChars.charAt(((_bytes[_bytes.length - 2] & 0x3) << 0x4) | (_bytes[_bytes.length - 1] >> 0x4)));
_result.push(this.EncodeChars.charAt((_bytes[_bytes.length - 1] & 0xF) << 0x2));
_result.push("=");
break;
}
return _result.join('');
},
DecodingBase64: function () {
var _len = this.str.length;
if (_len % 4 != 0) {
return "非base64编码";
}
var _regex = /^[A-Za-z0-9\+\/]+={0,2}$/;
if (!_regex.test(this.str)) {
return "包含不
正确的
BASE64编码,请检查";
}
var _byte = [];
var _3Dindex = this.str.indexOf("=");
if (_3Dindex != -1) {
_len = _3Dindex;
}
var _mod = _len % 4;
var _dev = Math.floor(_len / 4);
for (var i = 0; i < _dev; i++) {
var ch1 = this.EncodeChars.indexOf(this.str[4 * i]);
var ch2 = this.EncodeChars.indexOf(this.str[4 * i + 1]);
var ch3 = this.EncodeChars.indexOf(this.str[4 * i + 2]);
var ch4 = this.EncodeChars.indexOf(this.str[4 * i + 3]);
_byte.push((ch1 << 0x2) | (ch2 >> 0x4));
_byte.push(((ch2 & 0xF) << 0x4) | (ch3 >> 0x2));
_byte.push(((ch3 & 0x3) << 0x6) | ch4);
}
switch (_mod) {
case 2:
_byte.push((this.EncodeChars.indexOf(this.str.charAt(_len - 2)) << 0x2) | (this.EncodeChars.indexOf(this.str.charAt(_len - 1)) >> 0x4));

2011年11月13日 14点11分 15
level 8
abc😁😂 楼主
break;
case 3:
var _c = this.EncodeChars.indexOf(this.str.charAt(_len - 2));
_byte.push((this.EncodeChars.indexOf(this.str.charAt(_len - 3)) << 0x2) | (_c >> 0x4));
_byte.push(((_c & 0xF) << 4) | (this.EncodeChars.indexOf(this.str.charAt(_len - 1)) >> 0x2));
break;
}
i = 0;
var _str = [];
while (i < _byte.length) {
if ((_byte[i] >> 0x7) == 0x0) {
_str.push(this.DecodingUTF8([_byte[i]]));
i++;
}
else if ((_byte[i] >> 0x4) == 0xF) {
_str.push(this.DecodingUTF8([_byte[i], _byte[i + 1], _byte[i + 2], _byte[i + 3]]));
i += 4;
}
else if ((_byte[i] >> 0x5) == 0x7) {
_str.push(this.DecodingUTF8([_byte[i], _byte[i + 1], _byte[i + 2]]));
i += 3;
}
else if ((_byte[i] >> 0x6) == 0x3) {
_str.push(this.DecodingUTF8([_byte[i], _byte[i + 1]]));
i += 2;
}
else {
_str.push(this.DecodingUTF8([_byte[i]]));
i++;
}
}
return _str.join('');
}
};
function encoding() {
var base64 = new Base64();
base64.str = document.getElementById("infoarea").value;
document.getElementById("infoarea").value = base64.EncodingBase64();
}
function decoding() {
var base64 = new Base64();
base64.str = document.getElementById("infoarea").value;
document.getElementById("infoarea").value = base64.DecodingBase64();
}
</script>
</head>
<body>
<p>
<textarea id="infoarea" cols="80" rows="10"></textarea>
</p>
<p>
<input type="button" value="编码" onclick="encoding()" />
<input type="button" value="解码" onclick="decoding()" />
</p>
</body>
</html>
2011年11月13日 14点11分 16
level 8
abc😁😂 楼主
以上就是我用js写的base64编解码,完美支持Unicode
2011年11月13日 14点11分 17
level 7
你在说什么?
"非base64编码"
如果是unicode(UTF-16LE)编码
base64后是 XpdiAGEAcwBlADYANAAWfwF4
如果是GB2312(GBK)编码
base64后是 t8diYXNlNjSx4MLr
2011年11月13日 14点11分 18
level 7
如果是utf-8编码
base64后是 6Z2eYmFzZTY057yW56CB
2011年11月13日 14点11分 19
level 8
abc😁😂 楼主
我的意思是用Unicode得到XpdiAGEAcwBlADYANAAWfwF4
然后用UTF-8解开
2011年11月13日 14点11分 20
level 7
当然是乱码了.少年
2011年11月13日 14点11分 21
1 2 尾页