<HTML>
<HEAD>
<META name=VI60_defaultClientScript content=VBScript>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></HEAD>
<BODY>
<SCRIPT LANGUAGE=vbscript>
<!--
mystr="http://www.google.com/search?hl=zh-CN&ie=UTF-8&q=Visual+Basic+6.0%E4%B8%AD%E6%96%87%E7%89%88%E5%AE%9E%E7%94%A8%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C&btnG=%E6%90%9C%E7%B4%A2&lr=lang_zh-CN"
function getutf8(x)
'這個函數是用來得到%號的部分,
'輸入條件是""http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=%E5%85%B3%E9%94%AE%E5%AD%97&btnG=Google+Search"
dim first,last
A=split(x,"&")'定義一個臨時數組
dim i:i=0'臨時的指針
for i=0 to ubound(A)
if instr(A(i),"%")>0 then
first=instr(A(i),"%")
last=InStrRev(A(i),"%")
getutf8=getutf8 & mid(A(i),first,last-first+3)
end if
next
getutf8=right(getutf8,len(getutf8)-1)'去掉左邊的%
'msgbox getutf8
end function
msgbox U8toU(getutf8(mystr))
function c16to2(x)
'這個函數是用來轉換16進制到2進制的,可以是任何長度的,一般轉換UTF-8的時候是兩個長度,比如A9
'比如:輸入“C2”,轉化成“11000010”,其中1100是"c"是10進制的12(1100),那麼2(10)不足4位要補齊成(0010)。
dim tempstr
dim i:i=0'臨時的指針
for i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
do while len(tempstr)<4
tempstr="0" & tempstr'如果不足4位那麼補齊4位數
loop
c16to2=c16to2 & tempstr
next
end function
'document.write hex(asc("字")) & "<br/>"
function U8toU(x)
'輸入一堆有%分隔的字符串,先分成數組,根據utf8規則來判斷補齊規則
'輸入:關 E5 85 B3 鍵 E9 94 AE 字 E5 AD 97
'輸出:關 B9D8 鍵 BCFC 字 D7D6
dim WeiS'要判斷第一個編碼的位數
dim Unicode'二進制的Unicode碼
dim alpha'定義單個字符
A=split(x,"%")'定義一個臨時數組
dim i:i=0'臨時的指針
dim j:j=0'臨時的指針
for i=0 to ubound(A)
A(i)=c16to2(A(i))'第一次循環,先轉換成2進制再說
next
for i=0 to ubound(A)-1
WeiS=instr(A(i),"0")'判斷第一次出現0的位置,
'可能是1(單字節),3(3-1字節),4,5,6,7不可能是2和大於7
'理論上到7,實際不會超過3。
Unicode=""
for j=1 to WeiS-1
if j=1 then
A(i)=right(A(i),len(A(i))-WeiS)'第一個去掉最左邊的WeiS個
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)'其餘去掉最左邊的兩個
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode)) =4 then
U8toU=U8toU & chrw(int("&H" & c2to16(Unicode)))'總算完了,媽的!!
else
U8toU=U8toU & chr(int("&H" & c2to16(Unicode)))'總算完了,媽的!!
end if
next
end function
'msgbox c2to16("11100101")
function c2to16(x)
'2進制到16進制的轉換,每4個0或1轉換成一個16進制字母,輸入長度當然不可能不是4的倍數了
dim i:i=1'臨時的指針
for i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
next
end function
function c2to10(x)
'單純的2進制到10進制的轉換,不考慮轉16進制所需要的4位前零補齊。
'因爲這個函數很有用!以後也會用到,做過通訊和硬件的人應該知道。
'這裏用字符串代表二進制
c2to10=0
if x="0" then exit function'如果是0的話直接得0就完事
dim i:i=0'臨時的指針
for i= 0 to len(x) -1'否則利用8421碼計算,這個從我最開始學計算機的時候就會,好懷念當初教我們的謝道建老先生啊!
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
end function
function c10to2(x)
'10進制到2進制的轉換
dim sign, result
result = ""
'符號
sign = sgn(x)
x = abs(x)
if x = 0 then
c10to2 = 0
exit function
end if
do until x = "0"
result = result & (x mod 2)
x = x / 2
loop
result = strReverse(result)
if sign = -1 then
c10to2 = "-" & result
else
c10to2 = result
end if
end function
-->
</SCRIPT>
</BODY>
</HTML>