转移困惑 [英] Shift Confusion

查看:52
本文介绍了转移困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个字符打包成一个字节,而Python中的转移

让我感到困惑。


基本上,它应该可以在
Python中使用''packed string''格式,只要你发送的字符是ASCII

范围0到127 ,两个将适合一个字节。


这是代码。你能说出我做错了吗?


|导入类型

|

| def PackString(s):

| if type(s)!= types.StringType:

|提升异常(此例程仅包装字符串!)

| l = len(s)

|如果l%2!= 0:

| s = s +''\ 0''

| l + = 1

| chars = []

|我在范围内(0,l,2):

| x = ord(s [i])

| y = ord(s [i + 1])

| chars.append(chr((y << 1)| x))

|返回''''。join(字符)

|

| def UnpackString:

| if type(s)!= types.StringType:

| raise Exception(此例程仅解包字符串!)

| l = len(s)

| chars = []

|对于我在范围(l):

| temp = ord(s [i])

| c = 0xf0& temp

| chars.append(chr(c))

| c = 0x0f& temp

| chars.append(chr(c))

|返回''''。join(字符)

|

|

| def main():

| s =" Test string"

|打印s

| packed = PackString(s)

| print这是打包的字符串:"

|打印包装

| print"这是解压缩的字符串:"

|打印UnpackString(已打包)

|

| main()

解决方案

2005年2月23日22:06:54 -0800,Kamilche < kl ******* @ comcast.net>

写道:

我正在尝试将两个字符打包成一个字节Python中的转移使我感到困惑。

基本上,应该可以在Python中使用''packed string''格式,只要你发送的字符是ASCII
范围0到127,两个将适合一个字节。




它应该是可能的,但仅限于存在phlogiston和

永动机的领域。

保持一个ASCII字符0< = ord(c)< 128,你需要log2(128)== 7

位。一个字节有8位。因此,您只能在标准的8位字节中保存8 / 7.0 ==

1.14 ... ASCII字符。如果有一个像14位字节那样的b $ b的东西,这对我来说就是新闻。


其他你做错了:


1.使用L.lower()作为变量名。有些字体非常难以解决l1l1l1l1l1l1ll1l1l中的内容。 - 你能阅读

那个???


2.嗯:

< blockquote class =post_quotes>" chr((y<< 1)| x))" .upper()
''CHR((Y<<< 1)| X))' '


好​​吧,这是一个,而不是你的字符串的长度,增加。

要么是错的。移动一个角色留下一位(或者,

改变强调,一个BIT)然后在另一个角色中进行ORing

只有在你编写一个散列函数时才会模糊地合理。


3.假设这个现代炼金术有效:打开包装时,你怎么会知道字符串最初是(比方说)七个字符长? />
或8个字符长?


4.你的拆包程序似乎是试图打开两个4位

项目(半字节)超出一个字节,但没有做(temp& 0xf0)>> 4 for

正如人们所期待的那样,最高的蚕食..... aaahhh !! ??你试图用
来模拟包装小数吗???


5.不写下你要做什么的明确声明,

后跟输入和预期输出的示例。后者通过

等名称来代表测试驱动开发;当我开始编程

时,它被称为常识。


6.不以交互方式使用Python来探索正在发生的事情:<
ord(''x'')
120 ord(''y'')
121(120 << 1)
240(120< < 1)| 121
249




HTH,

John


在编程级别它似乎是正确的(主功能需要一个返回闭包

)。


但错误是恕我直言概念:

你需要7位的字符(从0到127或从x00到x7F的十六进制)

你不能只容纳一个其他字符!

其他128个符号(从128到255或从x80到xFF的十六进制)只有可能因为你再次使用而需要
比特,但是第8位设置

到1!


你想用C语言做什么(几年前......)

但是使用字节和单词,只在一个字中打包2个字节,但

你不能在一个字节中打包2个字符(每个字节几乎一个字节) !




" John Machin" < SJ ****** @ lexicon.net>在留言中写道

news:u8 ******************************** @ 4ax.com ...

基本上,应该可以在Python中使用''压缩字符串''格式,其中只要你发送的字符是ASCII
范围0到127,两个将适合一个字节。



它应该是可能的,但只能在一个领域存在phlogiston和
永动机。




alt.sys.pdp10?


I''m trying to pack two characters into a single byte, and the shifting
in Python has me confused.

Essentially, it should be possible to use a ''packed string'' format in
Python, where as long as the characters you''re sending are in the ASCII
range 0 to 127, two will fit in a byte.

Here''s the code. Can you tell what I''m doing wrong?

|import types
|
|def PackString(s):
| if type(s) != types.StringType:
| raise Exception("This routine only packs strings!")
| l = len(s)
| if l % 2 != 0:
| s = s + ''\0''
| l += 1
| chars = []
| for i in range(0, l, 2):
| x = ord(s[i])
| y = ord(s[i+1])
| chars.append(chr((y << 1) | x))
| return ''''.join(chars)
|
|def UnpackString(s):
| if type(s) != types.StringType:
| raise Exception("This routine only unpacks strings!")
| l = len(s)
| chars = []
| for i in range(l):
| temp = ord(s[i])
| c = 0xf0 & temp
| chars.append(chr(c))
| c = 0x0f & temp
| chars.append(chr(c))
| return ''''.join(chars)
|
|
|def main():
| s = "Test string"
| print s
| packed = PackString(s)
| print "This is the string packed:"
| print packed
| print "This is the string unpacked:"
| print UnpackString(packed)
|
|main()

解决方案

On 23 Feb 2005 22:06:54 -0800, "Kamilche" <kl*******@comcast.net>
wrote:

I''m trying to pack two characters into a single byte, and the shifting
in Python has me confused.

Essentially, it should be possible to use a ''packed string'' format in
Python, where as long as the characters you''re sending are in the ASCII
range 0 to 127, two will fit in a byte.



It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.

To hold one ASCII character 0 <= ord(c) < 128, you need log2(128) == 7
bits. There are 8 bits in a byte. Therefore you can hold only 8/7.0 ==
1.14... ASCII characters in a standard 8-bit byte. If there is such a
thing as a 14-bit byte, that''s news to me.

Other things you are doing wrong:

1. Using "L".lower() as a variable name. Some fonts make it extremely
hard to work out what is what in "l1l1l1l1l1l1ll1l1l" -- can you read
that???

2. Hmmm:

"chr((y << 1) | x))".upper() ''CHR((Y << 1) | X))''

OK so that''s a one, not the length of your string, as augmented.
Either would be be wrong. Shifting a character left ONE bit (or,
changing the emphasis, one BIT) and then ORing in another character
would be vaguely reasonable only if you were writing a hash function.

3. Supposing this modern alchemy had worked: when unpacking, how would
you know whether the string was originally (say) seven characters long
or 8 characters long?

4. Your unpacking routine appears to be trying to unpack two 4-bit
items (nibbles) out of a byte, but is not doing (temp & 0xf0) >> 4 for
the top nibble as one might expect ..... aaahhh!!?? are you trying to
emulate packed decimal???

5. Not writing down a clear statement of what you are trying to do,
followed by examples of input and expected output. This latter goes by
fancy names like "test-driven development"; when I started programming
it was known as "common sense".

6. Not using Python interactively to explore what''s going on:
ord(''x'') 120 ord(''y'') 121 (120 << 1) 240 (120 << 1) | 121 249



HTH,
John


At programming level it seems correct (a part a "return" closure
needed for the "main" function).

But the error is IMHO conceptual:
for a char you need 7 bits (from 0 to 127 or in hex from x00 to x7F)
and you can''t accomodate the other char in only one bit!
The other 128 symbols (from 128 to 255 or in hex from x80 to xFF) are
only possible because you use again 7 bits, but with the 8th bit set
to 1!

What you are trying to do I made in C language (some years ago...)
using however bytes and words, packing 2 bytes in only one word, but
you can''t pack 2 chars (each one beeing nearly a byte) in a byte!



"John Machin" <sj******@lexicon.net> wrote in message
news:u8********************************@4ax.com...

Essentially, it should be possible to use a ''packed string'' format in
Python, where as long as the characters you''re sending are in the ASCII
range 0 to 127, two will fit in a byte.



It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.



alt.sys.pdp10 ?


这篇关于转移困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆