或者,如果你马虎, >>(a 150 并且用于验证: >> int(''10010110'',2) 150 因此,可以将其作为函数包装 >> nibbles2byte = lambda a,b: 0xff& ; (((0xff& a)<<<<< 4)|(0xff& b)) >> nibbles2byte(a,b) 150 -tkc 2007年11月28日下午2:07,Gianmaria Iaculo - NVENTA < gi *** **** @ hotmail.com写的: 你好, 我是陌生的新手(来自.net所以请原谅我这个愚蠢的问题。 我想做一个非常简单的事情,有字节。 我的问题是: 我有一个字节,自然是由2个半字节组成的高和低,以及两个 字符...就像A和B.我想做的是将A写入高位半字节,将B $ / b $ b写入低位半字节。 python中的字符串是一个字节序列,所以你在描述 这里是字符串AB。 或者另一个例子可以是我有2个数字..比如7和8并且要做的是 与chars。 " \ x07 \ x08" 我真的很困惑怎么做,可能导致python是无类型的(动态 类型) 您可以使用struct模块转换回来在字节 序列和数值之间。例如,要获得一个带有 的整数,您之前提到的半字节值: struct.unpack(" h"," AB" ) - (16961,) 你想要使用什么以及你想要什么样的格式将取决于你为什么要这样做? 。 2007年11月28日下午2:27,Chris Mellon< ar ***** @ gmail.comwrote: 2007年11月28日下午2:07,Gianmaria Iaculo - NVENTA < gi ******* @ hotmail.comwrote: 你好, 我是陌生的新手(来自.net,请原谅我这个愚蠢的问题) 我要做一个非常简单的事情,用字节。 我的问题是: 我有一个字节,自然是由2个半字节组成的高和低,和两个 字符...像A和B.我想做的是写A给高位半字节和B 到较低的半字节。 python中的字符串是一个字节序列,所以你要描述的是 这里是字符串AB。 啊,直到我发送了你之后我还没有意识到你试图将b $ b合并到同一个字节。这并没有多大意义 - ord(A)超出你可以用半个字节表示的范围 - 但是 Python确实支持全方位的按位操作,因此你可以做任何类型的移动和设置,你在.NET中做了什么。 Hi there,I''m so new to python (coming from .net so excuse me for the stupid question)and i''m tring to do a very simple thing,with bytes.My problem is this:i''ve a byte that naturally is composed from 2 nibbles hi&low, and twochars.. like A nd B. What i wonna do is to write A to the High nibble and Bto the the lower nibble.Or an other example can be i''ve 2 numbers.. like 7 and 8 and whant to do thesame as for chars.I''m really confused on how t do it, maybe cause python is type-less (dynamictyped)Any Help?Cheers,GianmariaITALY 解决方案 I''m really confused on how t do it, maybe cause python istype-less (dynamic typed)Being duck-typed doesn''t really have anything to do with it.Python supports logical shifting and combiningi''ve a byte that naturally is composed from 2 nibbles hi&low,and two chars.. like A nd B. What i wonna do is to write A tothe High nibble and B to the the lower nibble. Or an otherexample can be i''ve 2 numbers.. like 7 and 8 and whant to dothe same as for chars.>>a = int(''1001'', 2)b = int(''0110'', 2)a9>>b6>>0xff & (((0xff & a) << 4) | (0xff & b))150or, if you''re sloppy,>>(a << 4) | b150And for verification:>>int(''10010110'', 2)150Thus, that can be wrapped up as a function>>nibbles2byte = lambda a,b:0xff & (((0xff & a) << 4) | (0xff & b))>>nibbles2byte(a,b)150-tkc On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA<gi*******@hotmail.comwrote:Hi there,I''m so new to python (coming from .net so excuse me for the stupid question)and i''m tring to do a very simple thing,with bytes.My problem is this:i''ve a byte that naturally is composed from 2 nibbles hi&low, and twochars.. like A nd B. What i wonna do is to write A to the High nibble and Bto the the lower nibble.A string in python is a sequence of bytes, so what you''re describinghere is the string "AB".Or an other example can be i''ve 2 numbers.. like 7 and 8 and whant to do thesame as for chars."\x07\x08"I''m really confused on how t do it, maybe cause python is type-less (dynamictyped)You can use the struct module to convert back and forth between bytesequences and numerical values. For example, to get an integer withthe value of the nibble you mentioned before:struct.unpack("h", "AB") -(16961,)Exactly what you''ll want to use and what format you want will dependon why you''re doing this.On Nov 28, 2007 2:27 PM, Chris Mellon <ar*****@gmail.comwrote:On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA<gi*******@hotmail.comwrote: Hi there, I''m so new to python (coming from .net so excuse me for the stupid question) and i''m tring to do a very simple thing,with bytes. My problem is this: i''ve a byte that naturally is composed from 2 nibbles hi&low, and two chars.. like A nd B. What i wonna do is to write A to the High nibble and B to the the lower nibble.A string in python is a sequence of bytes, so what you''re describinghere is the string "AB".Ah, I didn''t realize until after I''d sent this that you were trying tomerge them into the same byte. This doesn''t make a whole lot of sense- ord("A") is outside the range you can represent in half a byte - butPython does support the full range of bitwise operations, so you cando whatever kind of shifting and setting that you''d have done in .NET. 这篇关于位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 17:20