我知道如果将文件保存为UTF-8但却不知道如何将其保存为UTF-8(无BOM类型)表面看起来这两种格式没有区别,但如果用UE打开之后通过二进制格式看就会发现在UTF-8前面他会加一个FF FE,各位大侠有没有办法
解决方案 »
- 关于SQL执行多个表
- 在VB中生成一个表格,我想双击表格中的某一条记录,则这一条记录的内容会在一个新窗口中显示出来,应怎样做呀
- VB得到来访者外网IP的问题(标题说不清楚,请各位大大进来看看)
- 关于窗口显示的问题,大家帮帮忙啊!!!!!
- 真样把数据库中的表名及表结构显示并打印出来?
- 关于打包程序,很简单,在线等待
- 为何VB6不支持Access 2000 版的数据库?--在线等
- 使用mciSendString播放wav,但是现在程序正常运行(mciSendString返回值非0)但是没有声音,这是为什么?
- 我使用 recordset 的 find 方法不能定位到记录,请指教!!
- 刚刚完成一个6000行左右的学籍管理系统,有点感想
- 如何向QQ聊天窗口插入动态GIF图片呢?没思路
- 如何解决FTP远程文件复制
然后调用shell (type>filename)添加文件内容(我记不清楚>还是>>是添加到文件末尾了,自己试验换换看。)。
看看最后可以么?
' Convert a Unicode string to a byte stream of UTF-8
Dim BArray() As Byte
Dim TempB() As Byte
Dim i As Long
Dim k As Long
Dim TLen As Long
Dim b1 As Byte
Dim b2 As Byte
Dim UTF16 As Long
Dim j
TLen = Len(UniString) ' Obtain length of Unicode input string
If TLen = 0 Then Exit Function ' get out if there's nothing to convert
k = 0
For i = 1 To TLen
' Work out the UTF16 value of the Unicode character
CopyMemory b1, ByVal StrPtr(UniString) + ((i - 1) * 2), 1
CopyMemory b2, ByVal StrPtr(UniString) + ((i - 1) * 2) + 1, 1
' Combine the 2 bytes into the Unicode UTF-16
UTF16 = b2 ' assign b2 to UTF16 before multiplying by 256 to avoid overflow
UTF16 = UTF16 * 256 + b1
' Convert UTF-16 to 2 or 3 bytes of UTF-8
TempB = ToUTF8(UTF16)
' Copy the resultant bytes to BArray
For j = 0 To UBound(TempB)
ReDim Preserve BArray(k)
BArray(k) = TempB(j): k = k + 1
Next
ReDim TempB(0)
Next
UniStrToUTF8 = BArray ' Return the resultant UTF-8 byte arrayEnd Function
Function UTF8ToUniStr(BArray) As String
' Convert a byte stream of UTF-8 to Unicode String
Dim i As Long
Dim TopIndex As Long
Dim TwoBytes(1) As Byte
Dim ThreeBytes(2) As Byte
Dim AByte As Byte
Dim TStr As String
TopIndex = UBound(BArray) ' Number of bytes equal TopIndex+1
If TopIndex = 0 Then Exit Function ' get out if there's nothing to convert
i = 0 ' Initialise pointer
' Iterate through the Byte Array
Do While i <= TopIndex
AByte = BArray(i) ' fetch a byte
If AByte = &HE1 Then
' Start of 3 byte UTF-8 group for a character
' Copy 3 byte to ThreeBytes
ThreeBytes(0) = BArray(i): i = i + 1
ThreeBytes(1) = BArray(i): i = i + 1
ThreeBytes(2) = BArray(i): i = i + 1
' Convert Byte array to UTF-16 then Unicode
TStr = TStr & ChrW(ToUTF16(ThreeBytes))
ElseIf (AByte >= &HC3) And (AByte <= &HC6) Then
' Start of 2 byte UTF-8 group for a character
TwoBytes(0) = BArray(i): i = i + 1
TwoBytes(1) = BArray(i): i = i + 1
' Convert Byte array to UTF-16 then Unicode
TStr = TStr & ChrW(ToUTF16(TwoBytes))
Else
' Normal ANSI character - use it as is
TStr = TStr & Chr(AByte): i = i + 1 ' Increment byte array index
End If
Loop
UTF8ToUniStr = TStr ' Return the resultant string
End Function
Function HexDisplayOfFile(TFileName) As String
' Display the content of a text file in Hex format like:
' FF FE 54 00 B0 01 DB 1E 63 00
Dim Text1, MyChar, FileNum
FileNum = FreeFile ' Obtain a File handle from the OS
Open TFileName For Binary As #FileNum ' Open given Text file as binary
' Read all characters in the file.
Do While Not EOF(FileNum)
MyChar = Input(1, #FileNum) ' Read a character as raw binary
If MyChar <> "" Then
' Convert byte to Hex like 0A, 6B etc..
Text1 = Text1 & HexOf(Asc(MyChar)) & " "
End If
Loop
Close #FileNum ' Close file
HexDisplayOfFile = Text1 ' Return the Hex display string
End Function
Function GetFileEncoding(TFileName) As coEncoding
' Return the type of Text file : UTF16LE, UTF-8 or ANSI
Dim b1, FileNum
On Error Resume Next ' Ignore error
FileNum = FreeFile ' Obtain a File handle from the OS
Open TFileName For Binary As #FileNum ' Open given Textfile as Binary
' Read all characters in the file.
b1 = Input(1, #FileNum) ' Read the first character.
If Asc(b1) = &HFF Then
GetFileEncoding = coUnicode ' UTF-16LE
ElseIf Asc(b1) = &HEF Then
GetFileEncoding = coUTF8 ' UTF-8
Else
GetFileEncoding = coANSI ' Normal ANSI
End If
Close #FileNum ' Close the file
End Function
Function ToUniDecimal(UniString As String) As String
' Return the HTML equivalent string of a Unicode string
Dim i As Integer ' Must declare as integer for CopyMemory to work
Dim TLen, TStr
Dim b1 As Byte
Dim b2 As Byte
Dim UTF16 As Long
TLen = Len(UniString) ' Get Length of input Unicode string
If TLen = 0 Then Exit Function ' Get out if null string
' Iterate through each character in the string
For i = 1 To TLen
If IsUniChar(Mid(UniString, i, 1)) Then
' Cast the String character to 2 bytes
CopyMemory b1, ByVal StrPtr(UniString) + ((i - 1) * 2), 1
CopyMemory b2, ByVal StrPtr(UniString) + ((i - 1) * 2) + 1, 1
' Combine the 2 bytes into the Unicode UTF-16
UTF16 = b2 ' assign b2 to UTF16 before multiplying by 256 to avoid overflow
UTF16 = UTF16 * 256 + b1
' Convert UTF-16 to format 𘚟 for HTML
TStr = TStr & "&#" & Trim(CStr(UTF16)) & ";"
Else
' Get here if it;s an ANSI character
TStr = TStr & Mid(UniString, i, 1)
End If
Next
ToUniDecimal = TStr ' Return the HTML string
End Function
Function HexOf(ByVal AscNum As Integer) As String
' Return the 2 character Hex string of AscNum, prefix extra "0" if necessary
Dim TStr
If AscNum > 255 Then AscNum = AscNum Mod 256
TStr = Hex(AscNum) ' Convert to Hex
If Len(TStr) = 1 Then
' Attach "0" on the left
TStr = "0" & TStr
End If
HexOf = TStr ' Return the 2 character Hex string
End Function
Sub SaveUTF8(TStr)
' Save given Text string in UTF-8 format
Dim A(2) As Byte
Dim BArray() As Byte
Dim FileNum
' Place BOM of UTF-8 in first 3 bytes
A(0) = &HEF
A(1) = &HBB
A(2) = &HBF
' Delete output file if it exists
'If Dir(CommonDialog1.FileName) <> "" Then
' Kill CommonDialog1.FileName
'End If
FileNum = FreeFile ' Obtain a File handle from the OS
Open App.Path & "\xml\c.xml" For Binary As #FileNum
Put #FileNum, , A ' Write BOM bytes
' Convert the Unicode string to UTF-8 byte array
BArray = UniStrToUTF8(TStr)
Put #FileNum, , BArray ' Write byte array to file
Close #FileNum ' Close the file
End Sub
Sub SaveUTF82(TStr)
' Save given Text string in UTF-8 format
Dim A(2) As Byte
Dim BArray() As Byte
Dim FileNum
' Place BOM of UTF-8 in first 3 bytes
A(0) = &HEF
A(1) = &HBB
A(2) = &HBF
FileNum = FreeFile ' Obtain a File handle from the OS
Open App.Path & "\xml\userquery.temp" For Binary As #FileNum
Put #FileNum, , A ' Write BOM bytes
' Convert the Unicode string to UTF-8 byte array
BArray = UniStrToUTF8(TStr)
Put #FileNum, , BArray ' Write byte array to file
Close #FileNum ' Close the file
End Sub
Sub SaveUTF16(TStr)
' Save given Text string in UTF-16LE format
Dim i As Long, ab() As Byte
Dim TLen, FileNum
' Work out number of bytes required
TLen = Len(TStr) * 2
ReDim ab(TLen + 1) ' Prepare dimension of Byte array
' Place BOM of UTF-16LE in first 2 bytes
ab(0) = &HFF
ab(1) = &HFE
' Copy Unicode String to Byte array, 1 byte at a time
For i = 0 To TLen - 1
CopyMemory ab(i + 2), ByVal StrPtr(TStr) + i, 1
Next
' Delete output file if it exists
FileNum = FreeFile ' Obtain a File handle from the OS
Open App.Path & "\xml\userquery.temp" For Binary As #FileNum ' Open output file in binary
Put #FileNum, , ab ' Write byte array to file
Close #FileNum ' Close the file
End Sub
你的代码错误一大堆,不要什么代码都往上贴,
很伤人的
哎,又失望了