python2 python3 字符串 编码格式 处理

使用python的ctypes调用c语言中的函数,传入字符串,打印输出异常。解决方法记录于此。

参考连接:

http://blog.csdn.net/u011546806/article/details/44936303

主要原因是编码格式不同导致的。python2和python3采用的默认编码不同。

python2默认就是str和unicode,str和unicode可以直接进行连接。python3默认的字符串编码是bytes和str。如果要操作unicode格式的,需要通过encode()函数先转换。

c文件如下。

test.c

#include <stdio.h>

int test(char *temp)
{
    printf("temp:%s\n", temp);
    return 0;
}

test.py

#!/usr/bin/env python
import test
import os

from ctypes import *

test = cdll.LoadLibrary(os.getcwd() + '/libtest.so')
test.test('hello')

python2运行脚本输出如下

qt@tony:/mnt/hgfs/Desktop/$ /usr/bin/python2 test.py
temp:hello
temp:hello

python3运行结果

qt@tony:/mnt/hgfs/Desktop/$ /usr/bin/python3 test.py
temp:h
temp:h

解决方法,使用encode('utf-8')将字符串转化成unicode格式.

更改如下

#!/usr/bin/env python
import test
import os

from ctypes import *

test = cdll.LoadLibrary(os.getcwd() + '/libtest.so')
# 将str转换成unicode
test.test('hello'.encode('utf-8'))

Tony Liu

2017-6-2, Shenzhen

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值