[_UEF_Min] = UEF_ANSI
[_UEF_Max] = UEF_UTF32BE
End Enum
'ANSI+DBCS方式的文本所使用的代码页 。默认为0vb.netef,表示使用系统当前代码页 。可以利用该参数实现读取其vb.netef他代码编码的文本,比如想在 简体中文平台下 读取 繁体中文平台生成的txt,就将它设为950
Public UEFCodePage As Long
'判断BOM
'返回值:BOM所占字节
'dwFirst:[in]文件最开始的4个字节
'fmt:[out]返回编码类型
Public Function UEFCheckBOM(ByVal dwFirst As Long, ByRef fmt As UnicodeEncodeFormat) As Long
If dwFirst = HFEFF Then
fmt = UEF_UTF32LE
UEFCheckBOM = 4
ElseIf dwFirst = HFFFE0000 Then
fmt = UEF_UTF32BE
UEFCheckBOM = 4
ElseIf (dwFirst And HFFFF) = HFEFF Then
fmt = UEF_UTF16LE
UEFCheckBOM = 2
ElseIf (dwFirst And HFFFF) = HFFFE Then
fmt = UEF_UTF16BE
UEFCheckBOM = 2
ElseIf (dwFirst And HFFFFFF) = HBFBBEF Then
fmt = UEF_UTF8
UEFCheckBOM = 3
Else
fmt = UEF_ANSI
UEFCheckBOM = 0
End If
End Function
'生成BOM
'返回值:BOM所占字节
'fmt:[in]编码类型
'dwFirst:[out]文件最开始的4个字节
Public Function UEFMakeBOM(ByVal fmt As UnicodeEncodeFormat, ByRef dwFirst As Long) As Long
Select Case fmt
Case UEF_UTF8
dwFirst = HBFBBEF
UEFMakeBOM = 3
Case UEF_UTF16LE
dwFirst = HFEFF
UEFMakeBOM = 2
Case UEF_UTF16BE
dwFirst = HFFFE
UEFMakeBOM = 2
Case UEF_UTF32LE
dwFirst = HFEFF
UEFMakeBOM = 4
Case UEF_UTF32BE
dwFirst = HFFFE0000
UEFMakeBOM = 4
Case Else
dwFirst = 0
UEFMakeBOM = 0
End Select
End Function
'判断文本文件的编码类型
'返回值:编码类型 。文件无法打开时,返回UEF_Auto
'FileName:文件名
Public Function UEFCheckTextFileFormat(ByVal FileName As String) As UnicodeEncodeFormat
Dim hFile As Long
Dim dwFirst As Long
Dim nNumRead As Long
'打开文件
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ByVal 0)
If INVALID_HANDLE_VALUE = https://www.04ip.com/post/hFile Then'文件无法打开
UEFCheckTextFileFormat = UEF_Auto
Exit Function
End If
'判断BOM
dwFirst = 0
Call ReadFile(hFile, dwFirst, 4, nNumRead, ByVal 0)
nNumRead = UEFCheckBOM(dwFirst, UEFCheckTextFileFormat)
'Debug.Print nNumRead
'关闭文件
Call CloseHandle(hFile)
End Function
'读取文本文件
'返回值:读取的文本 。返回vbNullString表示文件无法打开
'FileName:[in]文件名
'fmt:[in,out]使用何种文本编码格式来读取文本 。为UEF_Auto时表示自动判断,且在fmt参数返回文本所用编码格式
Public Function UEFLoadTextFile(ByVal FileName As String, Optional ByRef fmt As UnicodeEncodeFormat = UEF_Auto) As String
Dim hFile As Long
Dim nFileSize As Long
Dim nNumRead As Long
Dim dwFirst As Long
Dim CurFmt As UnicodeEncodeFormat
Dim cbBOM As Long
Dim cbTextData As Long
Dim CurCP As Long
Dim byBuf() As Byte
Dim cchStr As Long
Dim I As Long
Dim byTemp As Byte
'判断fmt范围
If fmtUEF_Auto Then
If fmt[_UEF_Min] Or fmt[_UEF_Max] Then
GoTo FunEnd
End If
End If
'打开文件
hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, ByVal 0)
If INVALID_HANDLE_VALUE = https://www.04ip.com/post/hFile Then'文件无法打开
GoTo FunEnd
End If
'判断文件大小
nFileSize = GetFileSize(hFile, nNumRead)
If nNumRead0 Then '超过4GB
GoTo FreeHandle
推荐阅读
- 叮当直播录屏分享,叮当直播盒子
- 小区内服装缝补如何引流的简单介绍
- 手机发展的意义是什么意思,手机的发展带来了哪些方面的影响
- python注册函数 python如何写注册机
- 查看sqlserver连接,sqlserver 查看当前连接
- 画风清新单机游戏,画风清新单机游戏大全
- 英伟达显卡怎么看好坏,英伟达显卡怎么看出厂日期
- php数据库抛出异常 php抛出异常后是否会继续执行
- sap栏位,sap栏位点开内部错误表格格式