Python论坛  - 讨论区

标题:[python-chinese] simple Question.how convert char to byte?thanks

2006年12月19日 星期二 12:30

brightman fenyon在126.com
星期二 十二月 19 12:30:38 HKT 2006

Python 2.5 (r25:51908, Dec 18 2006, 21:43:53)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fp=open('test.flag','rb')
>>> c=fp.read(1)
>>> c&3
Traceback (most recent call last):
File "", line 1, in 
TypeError: unsupported operand type(s) for &: 'str' and 'int'
>>> c
'\x14'
>>> print c

>>> from ctypes import *
>>> a = c_char(c)
>>> a
c_char('\x14')
>>> a&3
Traceback (most recent call last):
File "", line 1, in 
TypeError: unsupported operand type(s) for &: 'c_char' and 'int'
>>> i=c_byte(c)
Traceback (most recent call last):
File "", line 1, in 
TypeError: int expected instead of str instance
>>> n=c_byte(a)
Traceback (most recent call last):
File "", line 1, in 
TypeError: int expected instead of c_char instance
>>> n=c_int(a)
Traceback (most recent call last):
File "", line 1, in 
TypeError: int expected instead of c_char instance
>>>


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月19日 星期二 12:42

M L liokmkoil在gmail.com
星期二 十二月 19 12:42:12 HKT 2006

¿ÉÒÔÊÔÒ»ÊÔarray»òÕßstructÄ£¿é

2006/12/19, brightman <fenyon在126.com>:
>
> Python 2.5 (r25:51908, Dec 18 2006, 21:43:53)
> [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> fp=open('test.flag','rb')
> >>> c=fp.read(1)
> >>> c&3
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: unsupported operand type(s) for &: 'str' and 'int'
> >>> c
> '\x14'
> >>> print c
> 
> >>> from ctypes import *
> >>> a = c_char(c)
> >>> a
> c_char('\x14')
> >>> a&3
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: unsupported operand type(s) for &: 'c_char' and 'int'
> >>> i=c_byte(c)
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: int expected instead of str instance
> >>> n=c_byte(a)
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: int expected instead of c_char instance
> >>> n=c_int(a)
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: int expected instead of c_char instance
> >>>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061219/3efd138b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月19日 星期二 12:53

limodou limodou在gmail.com
星期二 十二月 19 12:53:55 HKT 2006

On 12/19/06, brightman <fenyon在126.com> wrote:
> Python 2.5 (r25:51908, Dec 18 2006, 21:43:53)
> [GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> fp=open('test.flag','rb')
> >>> c=fp.read(1)
> >>> c&3
> Traceback (most recent call last):
> File "", line 1, in 
> TypeError: unsupported operand type(s) for &: 'str' and 'int'
> >>> c
> '\x14'

可以使用ord(c)将一个字符转为int呀。


-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月19日 星期二 14:10

Leo Jay python.leojay在gmail.com
星期二 十二月 19 14:10:33 HKT 2006

you may get the ascii code first, then do the bitwise operations.
following code for your information

>>> s = 'test'
>>> ord(s[0]) & 7
4
>>> ord(s[1]) & 7
5
>>> ord(s[2]) & 7
3
>>> ord(s[3]) & 7
4
>>>

-- 
Best Regards,
Leo Jay

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号