1樓:
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
private sub command1_click()
dim s as string, a() as byte
s = text1.text
a = encodetobytes(s)
for i = 0 to ubound(a)
text2.text = text2.text & hex(a(i)) & " "
next
end sub
public function encodetobytes(byval sdata as string) as byte()
dim aretn() as byte, 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
2樓:匿名使用者
vb裡漢字就是一個字元
msgbox (len("漢字"))這句顯示為2
在vb6中如何將utf-8編碼轉換為ansi編碼?
3樓:匿名使用者
先寫入檔案,再按對應的**頁按位元組讀取轉換。
**如下:
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
效果如下:
4樓:匿名使用者
重新改過,在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
在vb6中如何將utf8編碼轉換為ansi編碼
先寫入檔案,再按對應的 頁按位元組讀取轉換。如下 private const cp acp 0 default to ansi code page private const cp utf8 65001 default to utf 8 code page private declare funct...
php中如何把字串轉換為,PHP中如何把一個字串轉換為utf 8編碼的 如題 謝謝了
1 把 gbk 編碼字串轉換成 utf 8 編碼字串 view plaincopy to clipboardprint?text html charset utf 8 echo mb convert encoding 你是我的好朋友 utf 8 gbk 2 把 utf 8 編碼字串轉換成 gb231...
vb獲取字串中字元,vb獲取字串中字元
private sub command1 click dim b as integer,a as string,i as integer,c as string,d as string a text1.text c d for i 1 to len a b asc mid a,i,1 if b 65...