| 发表于: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 | | |
|