2的补码转换。这是正确的吗? [英] 2's complement conversion. Is this right?

查看:73
本文介绍了2的补码转换。这是正确的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个文件中读取3字节数字,然后签名(+8到

-8百万)。这似乎有效,但我不确定它是对的。


#将3个字符转换为数字。

Value1, Value2,Value3 = unpack("> BBB",Buffer [s:s + 3])

值=(Value1 * 65536)+(Value2 * 256)+ Value3

如果值> = 0x800000:

值 - = 0x1000000

打印价值


例如:

16682720 = -94496


应该是值 - = 0x1000001所以我得到-94497,而不是?


谢谢!


Bob

解决方案

2008-04-18,Bob Greschke< bo *@passcal.nmt.eduwrote:


我正在读取文件中的3字节数字并签名(+8到

-8百万)。这似乎有效,但我不确定它是对的。


#将3个字符转换为数字。

Value1, Value2,Value3 = unpack("> BBB",Buffer [s:s + 3])

值=(Value1 * 65536)+(Value2 * 256)+ Value3

如果值> = 0x800000:

值 - = 0x1000000

打印价值


例如:

16682720 = -94496


应该是值 - = 0x1000001所以我得到-94497,而不是?



不。 -94496是正确的答案:


>> -94496& 0xffffff



16682720


这是另一种方法:


---------------------------------------- --------------------------

import struct

def tohex (s):

返回''0x''+''''。join([''%0.2x''%ord(b)for b in s])


def from3Bytes(s):

#符号扩展s中的三个字节数使其长4个字节

如果ord(s [0 ])& 0x80:

s =''\ xff''+ s

else:

s =''\ x00''+ s

返回struct.unpack(''>我',s)[0]


for v in 0,1,-2,500,-500,7777 ,-7777,-94496,98765,-98765,8388607,-8388607,-8388608:

s = struct.pack(''> i'',v)[1:]

print"%8d%s%8d" %(v,tohex(s),from3Bytes(s))

---------------------------- --------------------------------------

-

格兰特爱德华兹格兰特哇!我会在那个'明亮的蓝色'中吃任何东西

!!

visi.com


Bob Greschke< bo*@passcal.nmt.eduwrote:


>我正在读取一个文件中的3字节数字并将它们签名(+8到
-8百万)。这似乎有效,但我不确定它是否正确。

#将3个字符转换为数字。

Value1,Value2,Value3 = unpack("> BBB",Buffer [s:s + 3])

Value =(Value1 * 65536)+(Value2 * 256)+ Value3

if值> = 0x800000:

值 - = 0x1000000

打印价值

例如:
16682720 = -94496
应该是值 - = 0x1000001,以便我得到-94497,而不是?



您的第一个案例是正确的,值 - = 0x1000000。值0xFFFFFFF

应该是-1和0xFFFFFFF - 0x1000000 == -1。


另一种方法:

Value = unpack("> l",Buffer [s:s + 3] +" \0")[0]> 8

Ross Ridge


-

l / // Ross Ridge - 伟大的HTMU

[oo] [oo] rr **** @ csclub.uwaterloo.ca

- () - /()/ < a rel =nofollowhref =http://www.csclub.uwaterloo.ca/~rridge/target =_ blank> http://www.csclub.uwaterloo.ca/~rridge/

db //


On 2008-04-18 14:37:21 -0600,Ross Ridge

< rr **** @ caffeine.csclub.uwaterloo.casaid:


Bob Greschke< bo*@passcal.nmt.eduwrote:
< blockquote class =post_quotes>
>我正在读取文件中的3字节数字,并且它们已经签名(+8到
-8百万)。这似乎有效,但我不确定它是否正确。

#将3个字符转换为数字。
Value1,Value2,Value3 = unpack(" > BBB",Buffer [s:s + 3])
值=(Value1 * 65536)+(Value2 * 256)+ Value3
如果Value> = 0x800000:
值 - = 0x1000000
打印价值

例如:
16682720 = -94496

应该是值 - = 0x1000001以便我得到-94497,而不是?



您的第一个案例是正确的,值 - = 0x1000000。值0xFFFFFFF

应该是-1和0xFFFFFFF - 0x1000000 == -1。


另一种方法:

Value = unpack("> l",Buffer [s:s + 3] +" \0")[0]> 8

Ross Ridge



很高兴知道(在数学方面从来没有好过)。


然而,在玩你的建议和格兰特的代码我已经

发现结构的东西比做这样的东西慢得多


值=(ord(Buf) [s])* 65536)+(ord(Buf [s + 1])* 256)+ ord(Buf [s + 2])

如果值> = 0x800000:

价值 - = 0x1000000


这几乎是坐在这里快两倍的两倍转换(比如3秒vs.~ 5秒只是依靠我的手指 - 在一个老太阳上...它有点慢。使用<< 16

替换* 65536,使用<< 8替换* 256可能会更快一些,但它太接近

来电没有真正描绘它。


我今天不打算做这个发现! :)


Bob


I''m reading 3-byte numbers from a file and they are signed (+8 to
-8million). This seems to work, but I''m not sure it''s right.

# Convert the 3-characters into a number.
Value1, Value2, Value3 = unpack(">BBB", Buffer[s:s+3])
Value = (Value1*65536)+(Value2*256)+Value3
if Value >= 0x800000:
Value -= 0x1000000
print Value

For example:
16682720 = -94496

Should it be Value -= 0x1000001 so that I get -94497, instead?

Thanks!

Bob

解决方案

On 2008-04-18, Bob Greschke <bo*@passcal.nmt.eduwrote:

I''m reading 3-byte numbers from a file and they are signed (+8 to
-8million). This seems to work, but I''m not sure it''s right.

# Convert the 3-characters into a number.
Value1, Value2, Value3 = unpack(">BBB", Buffer[s:s+3])
Value = (Value1*65536)+(Value2*256)+Value3
if Value >= 0x800000:
Value -= 0x1000000
print Value

For example:
16682720 = -94496

Should it be Value -= 0x1000001 so that I get -94497, instead?

Nope. -94496 is the right answer:

>>-94496 & 0xffffff

16682720

Here''s another way to do it:

------------------------------------------------------------------
import struct

def tohex(s):
return ''0x'' + ''''.join([''%0.2x'' % ord(b) for b in s])

def from3Bytes(s):
# sign extend the three byte number in s to make it 4 bytes long
if ord(s[0]) & 0x80:
s = ''\xff''+s
else:
s = ''\x00''+s
return struct.unpack(''>i'',s)[0]

for v in 0,1,-2,500,-500,7777,-7777,-94496,98765,-98765,8388607,-8388607,-8388608:
s = struct.pack(''>i'',v)[1:]
print "%8d %s %8d" % (v, tohex(s), from3Bytes(s))
------------------------------------------------------------------
--
Grant Edwards grante Yow! I''ll eat ANYTHING
at that''s BRIGHT BLUE!!
visi.com


Bob Greschke <bo*@passcal.nmt.eduwrote:

>I''m reading 3-byte numbers from a file and they are signed (+8 to
-8million). This seems to work, but I''m not sure it''s right.

# Convert the 3-characters into a number.
Value1, Value2, Value3 = unpack(">BBB", Buffer[s:s+3])
Value = (Value1*65536)+(Value2*256)+Value3
if Value >= 0x800000:
Value -= 0x1000000
print Value

For example:
16682720 = -94496

Should it be Value -= 0x1000001 so that I get -94497, instead?

Your first case is correct, "Value -= 0x1000000". The value 0xFFFFFFF
should be -1 and 0xFFFFFFF - 0x1000000 == -1.

An alternative way of doing this:

Value = unpack(">l", Buffer[s:s+3] + "\0")[0] >8

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //


On 2008-04-18 14:37:21 -0600, Ross Ridge
<rr****@caffeine.csclub.uwaterloo.casaid:

Bob Greschke <bo*@passcal.nmt.eduwrote:

>I''m reading 3-byte numbers from a file and they are signed (+8 to
-8million). This seems to work, but I''m not sure it''s right.

# Convert the 3-characters into a number.
Value1, Value2, Value3 = unpack(">BBB", Buffer[s:s+3])
Value = (Value1*65536)+(Value2*256)+Value3
if Value >= 0x800000:
Value -= 0x1000000
print Value

For example:
16682720 = -94496

Should it be Value -= 0x1000001 so that I get -94497, instead?


Your first case is correct, "Value -= 0x1000000". The value 0xFFFFFFF
should be -1 and 0xFFFFFFF - 0x1000000 == -1.

An alternative way of doing this:

Value = unpack(">l", Buffer[s:s+3] + "\0")[0] >8

Ross Ridge

Good to know (never was good on the math front).

However, in playing around with your suggestion and Grant''s code I''ve
found that the struct stuff is WAY slower than doing something like this

Value = (ord(Buf[s])*65536)+(ord(Buf[s+1])*256)+ord(Buf[s+2])
if Value >= 0x800000:
Value -= 0x1000000

This is almost twice as fast just sitting here grinding through a few
hundred thousand conversions (like 3sec vs. ~5secs just counting on my
fingers - on an old Sun...it''s a bit slow). Replacing *65536 with <<16
and *256 with <<8 might even be a little faster, but it''s too close to
call without really profiling it.

I wasn''t planning on making this discovery today! :)

Bob


这篇关于2的补码转换。这是正确的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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