VBS实现UTF-8转Unicode(UTF-16)

标签: , , , , ,

本文源于一个VBS群里的一个看起来很牛逼的人给别人的一个UTF-8转Unicode的函数Utf8ToUnicode。代码有60多行,放在了文章后面,以免喧宾夺主~

代码太长了,不知道是他自己写的还是复制粘贴的。我没有仔细看,但是简单测试了一下,貌似是正确的。

当时回了一句“Utf8ToUnicode函数哪需要这么复杂”,谁知他来了句“如果用api的话,当然一个了。”

算了,看在他的代码功能是正确的份上,我就不多说什么了。下面给出我写的自以为比他的简单的Utf8ToUnicode函数。

Function Utf8ToUnicode(str)
	Dim i, c, c2, c3, out, length
	out = ""
	i = 1
	length = LenB(str)
	Do While i <= length
		c = AscB(MidB(str,i,1))
		i = i + 1
		Select Case (c \ 2 ^ 4)
		    Case 0,1,2,3,4,5,6,7
		    	out = out & ChrW(c)
		    Case 12,13
		    	c2 = AscB(MidB(str,i,1))
		    	i = i + 1
		    	out = out & ChrW(((c And &H1F) * 2 ^ 6) Or (c2 And &H3F))
		    Case 14
		    	c2 = AscB(MidB(str,i,1))
		    	i = i + 1
		    	c3 = AscB(MidB(str,i,1))
		    	i = i + 1
		    	out = out & ChrW(((c And &H0F) * 2 ^ 12) Or _
		    			((c2 And &H3F) * 2 ^ 6) Or _
		    			((c3 And &H3F) * 2 ^ 0))
		End Select
	Loop
	Utf8ToUnicode = out
End Function

下面这个是他的代码o(╯□╰)o

Function Utf8ToUnicode(src)
	Dim IsAsc
	Dim utfLen
	utfLen = -1

	On Error Resume Next
	utfLen = LenB(src)
	If utfLen = -1 Then Exit Function
	On Error GoTo 0

	Dim i,j,k,N
	Dim B,cnt
	ReDim Buf(utfLen)
	i = 1
	j = 0
	Do While i <= utfLen
		B = AscB(MidB(src,i,1))
		If (B And &HFC) = &HFC Then
			cnt = 6
		ElseIf (B And &HF8) = &HF8 Then
			cnt = 5
		ElseIf (B And &HF0) = &HF0 Then
			cnt = 4
		ElseIf (B And &HE0) = &HE0 Then
			cnt = 3
		ElseIf (B And &HC0) = &HC0 Then
			cnt = 2
		Else
			cnt = 1
		End If

		If i + cnt - 1 > utfLen Then
			Buf(j) = "?"
			Exit Do
		End If

		Select Case cnt
			Case 2
				N = B And &H1F
			Case 3
				N = B And &HF
			Case 4
				N = B And &H7
			Case 5
				N = B And &H3
			Case 6
				N = B And &H1
			Case Else
				Buf(j) = Chr(B)
				IsAsc=True
		End Select

		If IsAsc = False Then
			For k = 1 To cnt - 1
				B = AscB(MidB(src,i+k,1))
				N = N * &H40 + (B And &H3F)
			Next
			Buf(j) = ChrW(N)
		End If

		i = i + cnt
		j = j + 1
		IsAsc = False
	Loop

	Utf8ToUnicode = Join(Buf, "")
End Function 
赞赏

微信赞赏支付宝赞赏

随机文章:

  1. 一个VBS恶作剧程序
  2. jQuery图片延迟加载插件——Lazy Load
  3. FireFox插件User Agent Switcher
  4. SDK编程中的窗口居中
  5. 你可以分配多大的内存?

一条评论 发表在“VBS实现UTF-8转Unicode(UTF-16)”上

  1. LJ说道:

    将你写的程序读了一遍,帅啊!!!

留下回复