使用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

python2 python3 字符串 编码格式 处理的更多相关文章

  1. Python3字符串 详解

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...

  2. 一篇文章助你理解Python2中字符串编码问题

    前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...

  3. python3字符串

    Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...

  4. python2 python3区别

    Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第 ...

  5. python2&python3

    1.Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi')   Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如 print 'hi ...

  6. python3 字符串属性(一)

    python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...

  7. 从python2,python3编码问题引伸出的通用编码原理解释

    今天使用python2编码时遇到这样一条异常UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef 发现是编码问题,但是平常在python3中几 ...

  8. (十四)Python3 字符串格式化

    Python3 字符串格式化 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用 ...

  9. python2 python3区别(续)

    1.除法 Python2 Python3 int/int → int int/int → float python2下整数除以整数返回整数类型,python3下整数除以整数返回浮点数类型 当某些语句假 ...

随机推荐

  1. SeekBar: 修改SeekBar中进度条的高度

    SeekBar中有两个很特别的属性需要留意下: 1.android:maxHeight和android:minHeight .前者是用来指定进度条最大高度的(此高度并非SeekBar整个控件的高度), ...

  2. Flink papers

    Around 2009 the Stratosphere research project started at the TU Berlin which a few years later was s ...

  3. 挂载ios,error tip:mount: wrong fs type, bad option, bad superblock on /dev/loop0,

    挂载ios,tip: mount -t iso9660 -o loop 111.iso /isofiles 有可能是-t参数有问题,把-t参数去掉,然后挂载,就成功了

  4. 高级数据库及一步一步搭建versant数据库

    总的来说,高级数据库课程分为分布式数据库和面向对象数据库两块.分布式数据库介绍了分布式数据库的方方面面,包括数据库系统的设计.查询处理优化.事务管理和恢复.并发控制.可靠性.安全性与目录管理等.面向对 ...

  5. canvas的图片绘制案例

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  6. java double类型保留两位小数4种方法

    http://blog.csdn.net/huaishuming/article/details/17752365 ****************************************** ...

  7. 【转】mysql 索引过长1071-max key length is 767 byte

    问题 create table: Specified key was too long; max key length is 767 bytes 原因 数据库表采用utf8编码,其中varchar(2 ...

  8. js实现类似微信网页版在可编辑的div中粘贴内容时过滤剪贴板的内容,光标始终在粘贴内容后面,以及将光标定位到最后的方法

    过滤剪贴板内容以及定位可编辑div光标的方法: <!DOCTYPE html><html lang="en"><head>  <meta ...

  9. Javascript 中ajax实现前台向后台交互

    第一种情况:前台传入字符串参数 后台返回json字符串.或是json数组  代码如下: 前台: $.ajax({ url: "xxx/xxx.action", data: &quo ...

  10. 遍历目录下的所有文件-os.walk

    #coding:utf-8 import os for root,dirs,files in os.walk("D:"): for fileItem in files: print ...