使用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. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  2. 微网站|h5弹窗|手机网站 html5 弹窗、弹层、提示框、加载条

    为了精确体验,您可通过Chrome设备模式浏览.或通过[url=]手机扫二维码进入[/url] layer mobile是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层 ...

  3. [Windows Azure] Using the Graph API to Query Windows Azure AD

    Using the Graph API to Query Windows Azure AD 4 out of 4 rated this helpful - Rate this topic This d ...

  4. c--日期和时间函数

    C的标准库<time.h>包含了一些处理时间与日期的函数. 1.clock_t clock(void); 函数返回程序自开始执行后的处理器时间,类型是clock_t,单位是tick.如果有 ...

  5. 关于emoji表情

    /** /* 表情编码 /* @param emoji:表情符号 **/ encodeEmoji: function (content) { var imgHtml = ""; v ...

  6. vue如何在路由跳转的时候更新组件

    项目中在路由跳转的时候碰到一个问题,没有更新视图,如何解决呢: https://segmentfault.com/a/1190000008879966 http://www.tuicool.com/a ...

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

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

  8. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  9. Machine Learning、Date Mining、IR&NLP 会议期刊论文推荐

    核心期刊排名查询 http://portal.core.edu.au/conf-ranks/ http://portal.core.edu.au/jnl-ranks/ 1.机器学习推荐会议 ICML— ...

  10. Python提取MD5

    使用Python的hashlib模块提取MD5,网上参考,觉得这个还不错,可以作为模块直接使用. # -*- coding: utf-8 -*- import hashlib import sys i ...