您的位置:程序门 -> vb -> 基础类



vb 读取 unicode utf-8 编码 文本文件问题???


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb 读取 unicode utf-8 编码 文本文件问题???[已结贴,结贴人:weitong918]
发表于:2007-05-09 14:47:27 楼主
在vb中使用objfso1.opentextfile   打开文本文件时,文件的打开方式为ansi,
现在需要处理的文件为unicode   或者   utf-8   编码方式,请问应该如何处理?

谢谢!!
发表于:2007-05-09 15:00:581楼 得分:5
http://www.chenoe.com/blog/article.asp?id=2030
发表于:2007-05-09 15:11:462楼 得分:10
模块     module_utf8

option   explicit
       
    public   m_bisnt           as   boolean
       
    public   declare   function   widechartomultibyte   lib   "kernel32 "   (byval   codepage   as   long,   byval   dwflags   as   long,   byval   lpwidecharstr   as   long,   byval   cchwidechar   as   long,   byref   lpmultibytestr   as   any,   byval   cchmultibyte   as   long,   byval   lpdefaultchar   as   string,   byval   lpuseddefaultchar   as   long)   as   long
    public   declare   function   multibytetowidechar   lib   "kernel32 "   (byval   codepage   as   long,   byval   dwflags   as   long,   byref   lpmultibytestr   as   any,   byval   cchmultibyte   as   long,   byval   lpwidecharstr   as   long,   byval   cchwidechar   as   long)   as   long
    public   declare   sub   copymemory   lib   "kernel32 "   alias   "rtlmovememory "   (lpvdest   as   any,   lpvsource   as   any,   byval   cbcopy   as   long)
       
    public   const   cp_utf8   =   65001
       
    'purpose:convert       utf8       to       unicode
    public   function   utf8_decode(byval   sutf8   as   string)   as   string
       
                dim   lngutf8size                               as   long
                dim   strbuffer                                       as   string
                dim   lngbuffersize                       as   long
                dim   lngresult                                       as   long
                dim   bytutf8()                                       as   byte
                dim   n                                                                       as   long
       
                if   lenb(sutf8)   =   0   then   exit   function
        m_bisnt   =   true       ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '     add     ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
                if   m_bisnt   then
                            on   error   goto   endfunction
                            bytutf8   =   strconv(sutf8,   vbfromunicode)
                            lngutf8size   =   ubound(bytutf8)   +   1
                            on   error   goto   0
                            'set       buffer       for       longest       possible       string       i.e.       each       byte       is
                            'ansi,       thus       1       unicode(2       bytes)for       every       utf-8       character.
                            lngbuffersize   =   lngutf8size   *   2
                            strbuffer   =   string$(lngbuffersize,   vbnullchar)
                            'translate       using       code       page       65001(utf-8)
                            lngresult   =   multibytetowidechar(cp_utf8,   0,   bytutf8(0),   _
                                        lngutf8size,   strptr(strbuffer),   lngbuffersize)
                            'trim       result       to       actual       length
                            if   lngresult   then
                                        utf8_decode   =   left$(strbuffer,   lngresult)
                            end   if
                else
                            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
                            dim   barray()                                           as   byte
       
                            'resume       on       error       in       case       someone       inputs       text       with       accents
                            'that       should       have       been       encoded       as       utf-8
                            on   error   resume   next
       
                            topindex   =   len(sutf8)                   '       number       of       bytes       equal       topindex+1
                            if   topindex   =   0   then   exit   function                               '       get       out       if       there 's       nothing       to       convert
                            barray   =   strconv(sutf8,   vbfromunicode)
                            i   =   0               '       initialise       pointer
                            topindex   =   topindex   -   1
                            '       iterate       through       the       byte       array
                            do   while   i   <=   topindex
                                        abyte   =   barray(i)
                                        if   abyte   <   &h80   then
                                                    '       normal       ansi       character       -       use       it       as       is
                                                    tstr   =   tstr   &   chr$(abyte):                       i   =   i   +   1                       '       increment       byte       array       index
                                        elseif   abyte   > =   &he0   then                                                       'was       =       &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$((threebytes(0)   and   &hf)   *   &h1000   +   (threebytes(1)   and   &h3f)   *   &h40   +   (threebytes(2)   and   &h3f))
                                        elseif   (abyte   > =   &hc2)   and   (abyte   <=   &hdb)   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$((twobytes(0)   and   &h1f)   *   &h40   +   (twobytes(1)   and   &h3f))
                                        else
                                                    '       normal       ansi       character       -       use       it       as       is
                                                    tstr   =   tstr   &   chr$(abyte):                       i   =   i   +   1                       '       increment       byte       array       index
                                        end   if
                            loop
                            utf8_decode   =   tstr                           '       return       the       resultant       string
                            erase   barray
                end   if
       
endfunction:
       
    end   function
       
发表于:2007-05-09 15:12:073楼 得分:10
接上


    'purpose:convert       unicode       string       to       utf-8.
    public   function   utf8_encode(byval   strunicode   as   string,   optional   byval   bhtml   as   boolean   =   true)   as   string
                dim   i                                                                       as   long
                dim   tlen                                                           as   long
                dim   lptr                                                           as   long
                dim   utf16                                                       as   long
                dim   utf8_encodelong               as   string
       
                tlen   =   len(strunicode)
                if   tlen   =   0   then   exit   function
       
                if   m_bisnt   then
                            dim   lngbuffersize                       as   long
                            dim   lngresult                                       as   long
                            dim   bytutf8()                                       as   byte
                            'set       buffer       for       longest       possible       string.
                            lngbuffersize   =   tlen   *   3   +   1
                            redim   bytutf8(lngbuffersize   -   1)
                            'translate       using       code       page       65001(utf-8).
                            lngresult   =   widechartomultibyte(cp_utf8,   0,   strptr(strunicode),   _
                                        tlen,   bytutf8(0),   lngbuffersize,   vbnullstring,   0)
                            'trim       result       to       actual       length.
                            if   lngresult   then
                                        lngresult   =   lngresult   -   1
                                        redim   preserve   bytutf8(lngresult)
                                        'copymemory       strptr(utf8_encode),       bytutf8(0&),       lngresult
                                        utf8_encode   =   strconv(bytutf8,   vbunicode)
                                        '       for       i       =       0       to       lngresult
                                        '                   utf8_encode       =       utf8_encode       &       chr$(bytutf8(i))
                                        '       next
                            end   if
                else
                            for   i   =   1   to   tlen
                                        '       get       utf-16       value       of       unicode       character
                                        lptr   =   strptr(strunicode)   +   ((i   -   1)   *   2)
                                        copymemory   utf16,   byval   lptr,   2
                                        'convert       to       utf-8
                                        if   utf16   <   &h80   then                                                                                                                                                                           '       1       utf-8       byte
                                                    utf8_encodelong   =   chr$(utf16)
                                        elseif   utf16   <   &h800   then                                                                                                                                                       '       2       utf-8       bytes
                                                    utf8_encodelong   =   chr$(&h80   +   (utf16   and   &h3f))                                                                                   '       least       significant       6       bits
                                                    utf16   =   utf16   \   &h40                                                                                                                                                               '       shift       right       6       bits
                                                    utf8_encodelong   =   chr$(&hc0   +   (utf16   and   &h1f))   &   utf8_encodelong                                           '       use       5       remaining       bits
                                        else                                                                                                                                                                                                                           '       3       utf-8       bytes
                                                    utf8_encodelong   =   chr$(&h80   +   (utf16   and   &h3f))                                                                                   '       least       significant       6       bits
                                                    utf16   =   utf16   \   &h40                                                                                                                                                               '       shift       right       6       bits
                                                    utf8_encodelong   =   chr$(&h80   +   (utf16   and   &h3f))   &   utf8_encodelong                                           '       use       next       6       bits
                                                    utf16   =   utf16   \   &h40                                                                                                                                                               '       shift       right       6       bits
                                                    utf8_encodelong   =   chr$(&he0   +   (utf16   and   &hf))   &   utf8_encodelong                                               '       use       4       remaining       bits
                                        end   if
                                        utf8_encode   =   utf8_encode   &   utf8_encodelong
                            next
                end   if
       
                'substitute       vbcrlf       with       html       line       breaks       if       requested.
                if   bhtml   then
                            utf8_encode   =   replace$(utf8_encode,   vbcrlf,   " <br/> ")
                end   if
       
    end   function
   


form中:

private   sub   command1_click()
dim   str_utf8           as   string
                    dim   str_unicode           as   string
                    open   "path "   for   input   as   1
                    str_utf8   =   strconv(inputb$(lof(1),   1),   vbunicode)                 '读到字符串
                    str_utf8   =   right(str_utf8,   len(str_utf8)   -   1)                           '第一个字符是格式标志,无实际意义,去掉
                    close   1
                    module_utf8.m_bisnt   =   true
                    str_unicode   =   utf8_decode(str_utf8)         '转换为unicode
                    msgbox   str_unicode
end   sub
发表于:2007-05-09 15:16:394楼 得分:10
刚才试着打开           rainstormmaster(暴风雨   v2.0)           给的网页

出现病毒???...
发表于:2007-05-09 15:19:485楼 得分:10
to   all:
zyl910的专栏给出了目前中国最好的vb读文本文件的sorce   code.
http://blog.csdn.net/zyl910/archive/2006/05/30/762693.aspx

另外,
http://www.chenoe.com/blog/article.asp?id=2030也是转自zyl910

to   rose2007():
在英文windows中,这一句有问题:
bytutf8   =   strconv(sutf8,   vbfromunicode)
最好用copymemory.
good   luck!
发表于:2007-05-09 15:26:276楼 得分:0
我的是转自坛里一个星星的     忘记他id了...
目前测试正常
谢谢vbadvisor(sunlight)的指点     我看下...

发表于:2007-05-09 15:29:477楼 得分:0
这么长~~
你那个文件是utf-8的嘛

一般新建的txt都是unicode的.

所以才会是12个字节.

要读的方法多了去了~~~~~连我所知道的就有两种.

这里给你一种简单的:

public   function   readfile(byval   filename   as   string)   as   string
        dim   objstream   as   object
        set   objstream   =   createobject( "adodb.stream ")
       
        with   objstream
                .type   =   2
                .mode   =   3
                .open
                .charset   =   "utf-8 "             '不同编码时自己换
                .loadfromfile   filename
                  readfile   =   .readtext
                .close
        end   with
end   function

直接调用readfile( "tmp1.txt "),返回值就是它的内容了.

我用这个好象也可以,谢谢了~~
发表于:2007-05-09 15:31:288楼 得分:0
这么长~~
你那个文件是utf-8的嘛
================================

你测试了吗?   怎么知道不是?
发表于:2007-05-09 15:32:379楼 得分:5
http://community.csdn.net/expert/topic/5515/5515190.xml?temp=.4686853
发表于:2007-05-09 15:33:0510楼 得分:0
.............................................
发表于:2007-05-09 15:36:3411楼 得分:0
晕死,居然不让我结贴~
csdn改规则了???
发表于:2007-05-09 15:40:2912楼 得分:0
rose2007()   不好意思,你误会了,

这下面的话是我从别的帖子转方法的时候,上面的一句话~~   没注意也转过来了~~   呵呵.
不是说你上面的方法~   不好意思哦~   谢谢!
-----------------------

你那个文件是utf-8的嘛

一般新建的txt都是unicode的.
---------------
发表于:2007-05-09 15:42:2213楼 得分:0
http://www.chenoe.com/blog/article.asp?id=2030

想看这个网站
怎么总有病毒...


快速检索

最新资讯
热门点击