自动识别文字的编码以及读取所有文本——VB2005
2009-11-16 20:35
399 查看
在VB2005中,读取文本文件中的文本可以采用的方法是System.IO.File.ReadAllText,这个函数有两个参数,一个是文件的文件名(包含路径);一个是文本的编码,如果省略,采用系统默认的编码。可是,一般的文本编码有ANSI、Unicode、UTF8等。如果编码的格式选择不对,则读出来是一段乱码。
笔者依照网上的资料,编写一个函数,能自动根据判别文本的编码格式,首先是将文本文件的数据读到字节数组中,再判别文本的编码格式,最后将字节数组转化为文本。
笔者依照网上的资料,编写一个函数,能自动根据判别文本的编码格式,首先是将文本文件的数据读到字节数组中,再判别文本的编码格式,最后将字节数组转化为文本。
Public Shared Function ReturnEncoding(ByVal tB() As Byte) As System.Text.Encoding Dim tB1 As Byte, tB2 As Byte, tB3 As Byte, tB4 As Byte If tB.Length < 2 Then Return Nothing tB1 = tB(0) tB2 = tB(1) If tB.Length >= 3 Then tB3 = tB(2) If tB.Length >= 4 Then tB4 = tB(3) If (tB1 = &HFE AndAlso tB2 = &HFF) Then Return System.Text.Encoding.BigEndianUnicode If (tB1 = &HFF AndAlso tB2 = &HFE AndAlso tB3 <> &HFF) Then Return System.Text.Encoding.Unicode If (tB1 = &HEF AndAlso tB2 = &HBB AndAlso tB3 = &HBF) Then Return System.Text.Encoding.UTF8 Return System.Text.Encoding.Default End Function
Public Shared Function ByteToText(ByVal mByte() As Byte) As String Dim tE As System.Text.Encoding = ReturnEncoding(mByte) Return tE.GetString(mByte) End Function
相关文章推荐
- 自动识别文字的编码以及读取所有文本——VB2005
- 关于自动识别文本流的编码方式
- IOS 实现TXT文本自动识别编码的方法。
- checkenc - 自动文本编码识别
- C语言自动识别文本编码
- php自动识别文字编码并转换为目标编码的方法
- 自动识别文字编码并且转换为目标编码(php 正则)
- 自动识别编码读取和写入文件
- IOS 实现TXT文本自动识别编码的方法
- php自动识别文字编码并转换为目标编码的方法
- iOS中读取文件夹下所有文本的行数
- 用javascript实现始终保持打开同一个子窗口以及关闭父窗口同时自动关闭所有子窗口
- 利用第三方软件识别图片文字并转换为文本
- 自动检测文本的字符集:URL的页面编码+文本文件编码
- C#自动识别文件编码
- 「post中文参数问题」以及「验证码自动识别备忘」
- VBS 批量读取文件夹内所有的文本到Excel的脚本
- 自动识别文件编码
- 韩国泛泰手机全系列L/K/S三版通刷自动识别手机版本脚本及使用说明以及其他
- android添加新的资源文件夹无法识别以及读取外置SD卡解决方案