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



求教:vb 中utf8字串(中英文)转gb3212或asni没有完全准确的办法吗?


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


求教:vb 中utf8字串(中英文)转gb3212或asni没有完全准确的办法吗?[已结贴,结贴人:fxs_2008]
发表于:2007-02-02 22:24:48 楼主
现在越来越多的网页用utf8编码。

对于utf8编码转换成gbk、或asni、gbk等vb能识别的的字符,是否有好办法?

我也查了不少方法能转,但仍有乱码!不是很准确,有没有转换无误的!


大家给指点个代码吧!
发表于:2007-02-02 22:40:251楼 得分:30
option   explicit

private   declare   function   multibytetowidechar   lib   "kernel32 "   (byval   codepage   as   long,   byval   dwflags   as   long,   byval   lpmultibytestr   as   long,   byval   cchmultibyte   as   long,   byval   lpwidecharstr   as   long,   byval   cchwidechar   as   long)   as   long
private   declare   function   widechartomultibyte   lib   "kernel32 "   (byval   codepage   as   long,   byval   dwflags   as   long,   byval   lpwidecharstr   as   long,   byval   cchwidechar   as   long,   byval   lpmultibytestr   as   long,   byval   cchmultibyte   as   long,   byval   lpdefaultchar   as   long,   byval   lpuseddefaultchar   as   long)   as   long

private   const   cp_acp   =   0                 '   default   to   ansi   code   page
private   const   cp_utf8   =   65001       '   default   to   utf-8   code   page

'字符转   utf8
public   function   encodetobytes(byval   sdata   as   string)   as   byte()   '   note:   len(sdata)   >   0
        dim   aretn()   as   byte
        dim   nsize   as   long
       
        nsize   =   widechartomultibyte(cp_utf8,   0,   strptr(sdata),   -1,   0,   0,   0,   0)   -   1
       
        if   nsize   =   0   then   exit   function
       
        redim   aretn(0   to   nsize   -   1)   as   byte
        widechartomultibyte   cp_utf8,   0,   strptr(sdata),   -1,   varptr(aretn(0)),   nsize,   0,   0
       
        encodetobytes   =   aretn
       
        erase   aretn
end   function

'   utf8   转字符
private   function   decodetobytes(byval   sdata   as   string)   as   byte()   '   note:   len(sdata)   >   0
        dim   aretn()   as   byte
        dim   nsize   as   long
       
        nsize   =   multibytetowidechar(cp_utf8,   0,   strptr(sdata),   -1,   0,   0)   -   1
       
        if   nsize   =   0   then   exit   function
       
       
        redim   aretn(0   to   2   *   nsize   -   1)   as   byte
        multibytetowidechar   cp_utf8,   0,   strptr(sdata),   -1,   varptr(aretn(0)),   nsize
       
        decodetobytes   =   aretn
       
        erase   aretn
end   function
发表于:2007-02-03 08:36:572楼 得分:0
你好,请问上面这个怎么用啊!

文件是utf8编码的。

我用open   后,如何解码?
发表于:2007-02-04 13:57:313楼 得分:5
请qq:369454807   跟我联系!
发表于:2007-02-04 14:08:444楼 得分:5
decodetobytes(utf8字符串)
返回的是一个ansi或者gb的byte数组,你再转回string不就可以了
发表于:2007-02-04 15:09:445楼 得分:0
仍不太明白,我查了很多,utf8转gb3212太难了!

我的文档是这样的:
partcode   =   inet1.openurl( "http://finance.phoenixtv.com/news/domestic/doclist.shtml ")
        open   "e:\zx\ "凤凰网国内.htm "   for   output   as   #1
        print   #1,   partcode
        close   #1
由于该网页是utf8编码,vb保存文档后打开就是乱码(特别是中文)!转成vb能识别的码太难了。
如何能将e:\zx\ "凤凰网国内.htm转换成能正常显示的文本呢?

此外,还有字节与字符的区别:
请问:decodetobytes返回字节数组,如何再转string字符

utf8字符串怎样才能获得呢?

发表于:2007-02-05 15:23:236楼 得分:0
太难了,我已基本解决了!
比如:
  open   "e:\zx\ "凤凰网国内.htm "   for   output   as   #1

open   "e:\zx\ "凤凰网国内.htm "   for   binary   as   #1
的区别等!

还有,什么情况下是字节数组?什么情况下是字符数组!
怎么用?

高手请再指点指点,就可以结贴了



快速检索

最新资讯
热门点击