Function UnicodeByteToUTF8Byte(UTF16() As Byte) As Byte()
Dim i As Long
Dim count As Long
Dim uSize As Long
Dim cpoint As Long
Dim tmp() As Byte, result() As Byte
uSize = (UBound(UTF16) - LBound(UTF16) + 1) \ 2
For i = 1 To uSize
CopyMemory cpoint, UTF16((i - 1) * 2 + LBound(UTF16)), 2
If cpoint < &H80 Then
ReDim tmp(1 To 1)
tmp(1) = cpoint
ElseIf cpoint < &H800 Then
ReDim tmp(1 To 2)
tmp(1) = &HC0 + ((cpoint \ &H40) And &H1F)
tmp(2) = &H80 + (cpoint And &H3F)
ElseIf (cpoint < &HD800) Or (cpoint > &HDFFF) Then
ReDim tmp(1 To 3)
tmp(3) = &H80 + (cpoint And &H3F)
cpoint = cpoint \ &H40
tmp(2) = &H80 + (cpoint And &H3F)
cpoint = cpoint \ &H40
tmp(1) = &HE0 + (cpoint And &HF)
End If
ReDim Preserve result(1 To count + UBound(tmp))
CopyMemory result(count + 1), tmp(1), UBound(tmp)
count = count + UBound(tmp)
Next
UnicodeByteToUTF8Byte = result
End Function
Function UnicodeToUTF8Byte(ByVal UTF16 As String) As Byte()
Dim uSize As Long
Dim m() As Byte
uSize = Len(UTF16)
If uSize = 0 Then Exit Function
ReDim m(1 To 2 * uSize)
CopyMemory m(1), ByVal StrPtr(UTF16), 2 * uSize
UnicodeToUTF8Byte = UnicodeByteToUTF8Byte(m)
End Function
Function UnicodeToUTF8(ByVal UniText As String) As String
Dim m() As Byte, n As Long
m = UnicodeToUTF8Byte(UniText)
For n = LBound(m) To UBound(m)
UnicodeToUTF8 = UnicodeToUTF8 & Chr(m(n))
Next
End Function
Function StringToUTF8String(ByVal text As String) As String
Dim uSize As Long, code As Long, s2 As String, s3 As String, result As String
uSize = Len(text)
If uSize = 0 Then Exit Function
For i = 1 To uSize
code = AscW(Mid(text, i, 1))
If code < &H80 Then
' 1 bai UTF-8
result = result & Chr(code)
ElseIf code < &H800 Then
' 2 bai UTF-8
result = result & Chr(&HC0 + ((code \ &H40) And &H1F)) & Chr(&H80 + (code And &H3F))
ElseIf (code < &HD800) Or (code > &HDFFF) Then
' 3 bai UTF-8
s3 = Chr(&H80 + (code And &H3F))
code = code \ &H40
s2 = Chr(&H80 + (code And &H3F))
code = code \ &H40
result = result & Chr(&HE0 + (code And &HF)) & s2 & s3
End If
Next
StringToUTF8String = result
End Function
Function UnicodeByteToUTF8(UTF16() As Byte) As String
Dim m() As Byte, n As Long
m = UnicodeByteToUTF8Byte(UTF16)
For n = LBound(m) To UBound(m)
UnicodeByteToUTF8 = UnicodeByteToUTF8 & Chr(m(n))
Next
End Function
Function UTF8ByteToUnicodeByte(UTF8() As Byte) As Byte()
Dim b1 As Byte, b2 As Byte, b3 As Byte
Dim index As Long
Dim result() As Byte, code As Long, count As Long, bytesRead As Long
index = LBound(UTF8)
ReDim result(0 To (UBound(UTF8) - LBound(UTF8)) * 2)
Do While index <= UBound(UTF8)
b1 = UTF8(index)
bytesRead = 1
If b1 < &H80 Then
code = b1
CopyMemory result(count), code, 2
count = count + 2
ElseIf (b1 And &HE0) = &HC0 Then
If index < UBound(UTF8) Then
b2 = UTF8(index + 1)
Else
b2 = 0
End If
If (b2 And &HC0) = &H80 Then
bytesRead = 2
code = (b1 And &H1F) * &H40 + (b2 And &H3F)
CopyMemory result(count), code, 2
count = count + 2
End If
ElseIf (b1 And &HF0) = &HE0 Then
If index < UBound(UTF8) Then
b2 = UTF8(index + 1)
Else
b2 = 0
End If
If (b2 And &HC0) = &H80 Then
bytesRead = 2
If index < UBound(UTF8) - 1 Then
b3 = UTF8(index + 2)
Else
b3 = 0
End If
If (b3 And &HC0) = &H80 Then
bytesRead = 3
code = (b1 And &HF)
code = code * &H1000 + (b2 And &H3F) * &H40 + (b3 And &H3F)
CopyMemory result(count), code, 2
count = count + 2
End If
End If
End If
index = index + bytesRead
Loop
ReDim Preserve result(0 To count - 1)
UTF8ByteToUnicodeByte = result
End Function
Function UTF8ByteToUnicode(UTF8() As Byte) As String
Dim m() As Byte, n As Long
m = UTF8ByteToUnicodeByte(UTF8)
n = (UBound(m) - LBound(m) + 1) \ 2
UTF8ByteToUnicode = String(n, &H20)
CopyMemory ByVal StrPtr(UTF8ByteToUnicode), m(LBound(m)), 2 * n
End Function
Function UTF8ToUnicode(ByVal UTF8 As String) As String
Dim m() As Byte, n As Long, count As Long
ReDim m(1 To Len(UTF8))
For n = 1 To Len(UTF8)
m(n) = Asc(Mid(UTF8, n, 1))
Next
m = UTF8ByteToUnicodeByte(m)
n = (UBound(m) - LBound(m) + 1) \ 2
UTF8ToUnicode = String(n, &H20)
CopyMemory ByVal StrPtr(UTF8ToUnicode), m(LBound(m)), 2 * n
End Function
Sub UTF8FileToUnicodeFile(ByVal utf_file As String, ByVal unicode_file As String)
Dim m() As Byte
Open utf_file For Binary As #1
ReDim m(0 To LOF(1) - 1)
Get #1, , m
Close #1
m = UTF8ByteToUnicodeByte(m)
Open unicode_file For Binary As #1
Put #1, , m
Close #1
End Sub
Sub UnicodeFileToUTF8File(ByVal unicode_file As String, ByVal utf_file As String)
Dim m() As Byte
Open unicode_file For Binary As #1
ReDim m(0 To LOF(1) - 1)
Get #1, , m
Close #1
m = UnicodeByteToUTF8Byte(m)
Open utf_file For Binary As #1
Put #1, , m
Close #1
End Sub