当前位置:编程学习 > python >>

iconv python

#encoding: gbk

from ctypes import *

iconv = cdll.libiconv2

www.zzzyk.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的专栏
 

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,