7,785
社区成员




Public Sub ReadAll(sTempFile As String, sAll As String)
If sTempFile = "" Then
Exit Sub
End If
Dim lFreeHandle As Long
lFreeHandle = FreeFile
Open sTempFile For Input As #lFreeHandle
sAll = Input(LOF(lFreeHandle), lFreeHandle)
Close #lFreeHandle
End Sub
Public Sub WriteAll(sTempFile As String, sAll As String)
If sTempFile = "" Then
Exit Sub
End If
Dim lFreeHandle As Long
lFreeHandle = FreeFile
Open sTempFile For Output As #lFreeHandle
Print #lFreeHandle, sAll
Close #lFreeHandle
End Sub
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
Else
Utf8ToUnicode = ""
End If
End Function
Public Function UnicodeFile_Read_VB(ByVal sFileName As String, _
Optional ByVal bRemoveBOM As Boolean) As String
Dim FF As Long
Dim b() As Byte
Dim s As String
Const uBOM As String = "ÿþ"
On Error Resume Next
FF = FreeFile
Open sFileName For Binary Access Read As FF
ReDim b(LOF(FF))
Get FF, , b
Close FF
s = b
If bRemoveBOM Then
If InStr(s, uBOM) = 1 Then
s = Replace$(s, uBOM, "")
End If
End If
b=s
UnicodeFile_Read_VB = FromMultiByteb(b, 65001)
End Function
Public Function FromMultiByteb(ByRef b() As Byte, ByVal CodePage As Long) As String
Dim lngOutLen As Long
Dim strWide As String
lngOutLen = MultiByteToWideChar(CodePage, _
0&, _
VarPtr(b(LBound(b))), _
UBound(b) - LBound(b) + 1, _
0, _
0)
If lngOutLen = 0 Then
Err.Raise vbObjectError Or &HC312&, _
"modHelpers.FromMultiByteb", _
"Failed to decode string, system error " _
& CStr(Err.LastDllError)
Else
strWide = String$(lngOutLen, 0)
lngOutLen = MultiByteToWideChar(CodePage, _
0&, _
VarPtr(b(LBound(b))), _
UBound(b) - LBound(b) + 1, _
StrPtr(strWide), _
lngOutLen)
If lngOutLen = 0 Then
Err.Raise vbObjectError Or &HC312&, _
"modHelpers.FromMultiByteb", _
"Failed to decode string, system error " _
& CStr(Err.LastDllError)
Else
FromMultiByteb = strWide
End If
End If
End Function
Dim Temp() As Byte
Dim Templen As Long
Dim I As Long
Templen = FileLen(sFilePath1)
ReDim Temp(Templen) As Byte
Open sFilePath1 For Binary As #1
Get #1, , Temp
Close #1
sFileContent = Utf8ToUnicode(Temp)