在vb6中如何將utf8編碼轉換為ansi編碼

2022-01-05 05:41:01 字數 4026 閱讀 5661

1樓:匿名使用者

先寫入檔案,再按對應的**頁按位元組讀取轉換。

**如下:

private const cp_acp = 0        ' default to ansi code page

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

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 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)

redim aretn(0 to nsize - 1) as byte

widechartomultibyte cp_utf8, 0, strptr(sdata), -1, varptr(aretn(0)), nsize, 0, 0

encodetobytes = aretn

end function

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)

redim aretn(0 to 2 * nsize - 1) as byte

multibytetowidechar cp_utf8, 0, strptr(sdata), -1, varptr(aretn(0)), nsize

decodetobytes = aretn

end function

private sub command1_click()

dim adata() as byte

dim sdata as string

dim nfilenumber as integer

open "c:\1.txt" for output as #1

print #1, text1

close #1

nfilenumber = freefile()

open "c:\1.txt" for binary as nfilenumber

redim adata(0 to lof(nfilenumber) - 1) as byte

get nfilenumber, 1, adata

close nfilenumber

sdata = decodetobytes(adata)

text2.text = sdata

end sub

效果如下:

2樓:匿名使用者

重新改過,在xp已經過測試可用

option explicit

private 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

private 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

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

endfunction:

end function

private sub command1_click()

dim str_utf8 as string

dim str_unicode as string

open "c:\utf8.txt" for binary as 1

seek #1, 1

str_utf8 = strconv(inputb$(lof(1), 1), vbunicode) '讀到字串

str_utf8 = right(str_utf8, len(str_utf8) - 1) '第一個字元是格式標誌,無實際意義,去掉

close 1

str_unicode = utf8_decode(str_utf8) '轉換為unicode

text1.text = str_unicode '顯示到文字框

'儲存到unicode格式檔案

open "c:\ansi.txt" for binary as #1

put #1, , str_unicode

close #1

end sub

如何將utf-8編碼的csv檔案轉換為gbk編碼

vb中如何將字串轉換為utf 8編碼

private declare function multibytetowidechar lib kernel32 byval codepage as long,byval dwflags as long,byval lpmultibytestr as long,byval cchmultibyte...

vb6中怎麼修改字型顏色,VB中如何改變文字顏色

可以直接在bai 屬性面板裡設定對 象的du zhiforecolor 如果有 屬性。用 設定dao,其內格式 容物件.forecolor 設定值其中設定值可以為四種 1 例如 物件.forecolor vbred 紅色,或vbblue等,vbgreen綠色等 2 rgb 正如樓上 爛掉 蘿蔔 所言...

VB中如何把數字轉換成字母,VB如何將26個把字母變為數字

chr 65 在vb中.可以使用asc a 來得到字元的asc碼,也可以反過來使用chr 65 將asc碼轉回到字元 暈chr 65 就會輸出a asc a 就會輸出65 兩者的轉換 private sub mand1 click msgbox chr 65 end sub private sub ...