iconv python


#encoding: gbk

from ctypes import *

iconv = cdll.libiconv2

www.2cto.com

print(iconv)

def lst(cnt, names, data):

    print("COUNT:", cnt)

    for i in range(cnt):

        print(names[i])

 

cb = CFUNCTYPE(None, c_int, POINTER(c_char_p), c_void_p)(lst)

iconv.libiconvlist(cb, 0)

h = iconv.libiconv_open(b"GBK", b"UCS-2LE")

print("HANDLE:", h)

buf = c_buffer(1024)

wbuf = (c_wchar*1024)()

wbuf.value = "中文字符串,测试ICONV用"

la = c_int(24)

lw = c_int(len(wbuf.value) * 2)

lpwbuf = POINTER(c_wchar_p)(cast(wbuf, c_wchar_p))

lpbuf = POINTER(c_char_p)(cast(buf, c_char_p))

iconv.libiconv(h, lpwbuf, byref(lw), lpbuf, byref(la))

print("INPUT LEFT:", lw.value)

print("OUTPUT LEFT:", la.value)

print("OUTPUT LEN:", len(buf.value))

print(buf.value.decode("gbk"))

iconv.libiconv_close(h)

摘自 lqefn的专栏
 

相关内容

    暂无相关文章

评论关闭