Write UTF-8 File From VB6

Hi, I am Arren.
Maybe you need output file as UTF-8 encoding, but VB6 is nativly support ansi encoding, it's depend on your machine.  I write this module, it can help you write utf-8 encoding files from vb6.
Option  Base  0

Public   Function  OpenFile(path  As   String As   Integer
    
Kill  path
    
Dim  fid  As   Integer
    fid 
=   FreeFile ()
    Open path 
For  Binary  As  fid
    OpenFile 
=  fid
End Function

Public   Sub  CloseFile(fid  As   Integer )
    Close fid
End Sub


Public   Function  WriteFile(fid  As   Integer , s  As   String As   Integer
    
Dim  bs()  As   Byte , buf( 0   To   6 As   Byte
    
Dim  c  As   Integer , l  As   Integer , i  As   Integer , ch  As   Long
    
    bs 
=  s
    c 
=   UBound (bs)
    
    
For  i  =   0   To  c  Step   2
        ch 
=  bs(i  +   1 *   2   ^   8   Or  bs(i)
        l 
=  UnicodeToUTF8(ch, buf)
        
Call  WriteBytes(buf, l, fid)
    
Next  i
End Function

Private   Sub  WriteBytes(buf()  As   Byte , cnt  As   Integer , fid  As   Integer )
    
Dim  i  As   Integer
    
Dim  b( 0   To   0 As   Byte
    
For  i  =   0   To  cnt
        b(
0 =  buf(i)
        Put fid, , b
    
Next  i
End Sub

Private   Function  UnicodeToUTF8(ch  As   Long , buf()  As   Byte As   Integer
    
Dim  i  As   Integer : i  =   0
    
If  (ch  <   & H80)  Then
        buf(i) 
=  ch
    
ElseIf  (ch  <   & H800)  Then
        buf(i) 
=   & HC0  Or  ((ch  And   & H7C00)  /   2   ^   6 ): i  =  i  +   1
        buf(i) 
=   & H80  Or  (ch  And   & H3F)
    
ElseIf  (ch  <   & H10000)  Then
        buf(i) 
=   & HE0  Or  ((ch  And   & HF000)  /   2   ^   12 ): i  =  i  +   1
        buf(i) 
=   & H80  Or  ((ch  And   & HFC0)  /   2   ^   6 ): i  =  i  +   1
        buf(i) 
=   & H80  Or  (ch  And   & H3F)
    
End   If
    UnicodeToUTF8 
=  i
End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值