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
python2 python3 字符串 编码格式 处理的更多相关文章
- Python3字符串 详解
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...
- 一篇文章助你理解Python2中字符串编码问题
前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
- python2 python3区别
Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第 ...
- python2&python3
1.Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi') Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如 print 'hi ...
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
- 从python2,python3编码问题引伸出的通用编码原理解释
今天使用python2编码时遇到这样一条异常UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef 发现是编码问题,但是平常在python3中几 ...
- (十四)Python3 字符串格式化
Python3 字符串格式化 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用 ...
- python2 python3区别(续)
1.除法 Python2 Python3 int/int → int int/int → float python2下整数除以整数返回整数类型,python3下整数除以整数返回浮点数类型 当某些语句假 ...
随机推荐
- EditText: 自定义EditText 触摸时无法获取焦点
写了一个自定义View,继承EditText,但是在触摸时无法获取到焦点. 在XML中添加 android:focusableInTouchMode="true" 设置触摸时可以获 ...
- javascript基础拾遗(一)
1.判断变量类型 var num = '123'; if(typeof num == 'number'){ alert('this is a number'); } else{ throw 'this ...
- 什么是内存溢出以及java中内存泄漏5种情况的总结
内存泄漏定义(memory leak):一个不再被程序使用的对象或变量还在内存中占有存储空间. 一次内存泄漏似乎不会有大的影响,但内存泄漏堆积后的后果就是内存溢出.内存溢出 out of memory ...
- TortoiseSVN checkout 之后图标(绿色勾之类的)没有显示出来的问题
http://blog.csdn.net/xigu_233/article/details/44595547 ********************************************* ...
- Gridview排序与分页-不使用“DataSourceControl DataSource”的情况下如何分页和排序 ...
如果你在GridView控件上设置 AllowPaging="true" or AllowSorting="true" 而没有使用使用数据源控件 DataSou ...
- [转]mybatis if test非空判断数字0为什么是false
原文地址:http://blog.51cto.com/wangguangshuo/1944531 今天工作中发现一个Long类型的参数没有传到sql中去,在sql xml配置文件中是使用if test ...
- Extjs gridpanel 合并单元格
/* * *合并单元格的函数,合并表格内所有连续的具有相同值的单元格.调用方法示例: * *store.on("load",function(){gridSpan(grid,&qu ...
- java泛型中<? super String>和<? extends String> 的区别
(1)<? super String> is any class which is a superclass of String (including String itself). (I ...
- Py2.7 no module named tkinter
一个简单的例子 #! /usr/bin/env python#coding=utf-8from tkinter import *Label(text="Spam").pack()m ...
- sam9260 adc 测试
/* * adc_test.c * * Copyright (C) 2007 Mengrz */ #include <stdio.h> #include <stdlib.h> ...