代码如下:
===========================================
复制代码 代码如下:
<script language="vbscript">
'http://www.51sjk.com/Upload/Articles/1/0/254/254520_20210628000952141.html
public function utf8encodechar(z)
dim c : c=ascw(z)'取unicode编码
if c>0 and c<256 then'asc编码直接返回
utf8encodechar=z
exit function
end if
if c<0 then c=c + &h10000&'vbscript的integer溢出,加上
dim k : k=clng(c)'备份一个编码,后面判断要用
dim b()
dim i : i=0
while c>&h0&'将编码按照6位一组,分组存到字节数组 b 中
redim preserve b(i)
b(i)=cbyte(c and &h3f&)
c=c \ &h40&
i=i+1
wend
if ubound(b)>0 then '如果分开的6位组不止一个,除最高一组外,全部加上二进制10000000
for i=0 to ubound(b)-1
b(i)=b(i) + &h80
next
end if
i=ubound(b)'根据字符的unicode编码范围,给最高组加上前缀
if k<=clng(&h7f&) then
b(i) = b(i) + 0
elseif k<=clng(&h7ff&) then
b(i) = b(i) + &hc0
elseif k<=clng(&hffff&) then
b(i) = b(i) + &he0
elseif k<=clng(&h1fffff&) then
b(i) = b(i) + &hf0
elseif k<=clng(&h3ffffff&) then
b(i) = b(i) + &hf8
else
b(i) = b(i) + &hfc
end if
utf8encodechar=""
for i=ubound(b) to 0 step -1'将分组转换成url编码
utf8encodechar=utf8encodechar & "%" & right("00" & hex(b(i)),2)
next
erase b
end function
public function utf8encodestring(s)
dim i,l,c : l=len(s)
for i=1 to l
utf8encodestring=utf8encodestring & utf8encodechar(mid(s,i,1))
next
end function
msgbox utf8encodestring("圪圪 eglic ")
</script>
测试方法:
http://www.google.com/search?hl=zh-cn&newwindow=1&rls=gglg%2cgglg%3a2006-15%2cgglg%3azh-cn&q=你的编码
复制代码 代码如下:
function revertutf8(szinput)
{
var x,wch,wch1,wch2,uch="",szret="";
for (x=0; x<szinput.length; x++)
{
if (szinput.charat(x)=="%")
{
wch =parseint(szinput.charat(++x) + szinput.charat(++x),16);
if (!wch) {break;}
if (!(wch & 0x80))
{
wch = wch;
}
else if (!(wch & 0x20))
{
x++;
wch1 = parseint(szinput.charat(++x) + szinput.charat(++x),16);
wch = (wch & 0x1f)<< 6;
wch1 = wch1 & 0x3f;
wch = wch + wch1;
}
else
{
x++;
wch1 = parseint(szinput.charat(++x) + szinput.charat(++x),16);
x++;
wch2 = parseint(szinput.charat(++x) + szinput.charat(++x),16);
wch = (wch & 0x0f)<< 12;
wch1 = (wch1 & 0x3f)<< 6;
wch2 = (wch2 & 0x3f);
wch = wch + wch1 + wch2;
}
szret += string.fromcharcode(wch);
}
else
{
szret += szinput.charat(x);
}
}
return(szret);
}
function u2utf8($c)
{
/*for($i=0;$i<count($c);$i++)*/
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=chr(0xc0 | $c>>6);
$str.=chr(0x80 | $c & 0x3f);
}
else if ($c < 0x10000) {
$str.=chr(0xe0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3f);
$str.=chr(0x80 | $c & 0x3f);
}
else if ($c < 0x200000) {
$str.=chr(0xf0 | $c>>18);
$str.=chr(0x80 | $c>>12 & 0x3f);
$str.=chr(0x80 | $c>>6 & 0x3f);
$str.=chr(0x80 | $c & 0x3f);
}
return $str;
}
1
12下一页