Python中的16位人工总和

时间:2019-03-27 18:46:11

标签: python bit-manipulation bit bitstring

我正在开发一个程序,该程序正在为立方体卫星项目创建IRIG106第10章数据。目前,它正在用python实现,而我在实现第10章标题的最后一个组件时遇到了困难。

我实现它的方式,我目前发现的校验和值大于规范所定义的大小整数(2个字节)内的校验和值。

该标准在IRIG 106-09 standard的10.6.1.1节“ J”段中定义了头校验和。定义如下:

J标头校验和。 (2个字节)包含一个值,该值表示报头中除报头校验和字之外的所有16位字的16位算术和。

还提供了programming manual,其中包含示例C代码,该代码显示以下内容(来自A-2-17页):

def calculate_checksum(byte_data: BitArray = None, header_length_bytes: int = 24, chunk_length: int = 16): 

    # Set the checksum to zero:
    checksum = 0

    # Loop through the Chapter 10 header and compute the 16 bit arithmetic sum:
    for bit_location in range(0, (header_length_bytes-2), chunk_length):

        # Get the range of bits to select:
        bit_range = slice(bit_location, (bit_location + chunk_length))

        # Get the uint representation of the bit data found: 
        checksum += Bits(bin=byte_data.bin[bit_range]).uint

    # Write the computed checksum as binary data to the start location of the checksum in the header: 
    byte_data.overwrite(Bits(uint=checksum, length=chunk_length), (header_length_bytes-2*8))

我已经使用BitString library在Python中实现了以下内容:

test_value = 2**16
test_value1 = test_value + 500
test_value2 = test_value1 % (2**16) -> 500
test_value3 = test_value1 & 0xFFFF -> 500

非常感谢您提供的任何想法或见解。我知道这应该是一个简单的解决方案,但我看不到它。

---更新2 --- 我尝试过翻转和截断,它们都产生了相同的结果:

Sync = "EB25" (2 bytes)
ChannelID = 1 (2 bytes)
PacketLen = 1024 (4 bytes) 

---更新3 ---

当我比较python和C校验和函数的执行时,根据规范,使用这些值作为输入时,我遇到了以下问题:

Header0: EB25                                                                                                                                                                                                               
 index =  0 16bit chunk = 60197 checksum = 60197                                                                                                                                                                            
Header1: 0001                                                                                                                                                                                                               
 index =  1 16bit chunk =     1 checksum = 60198                                                                                                                                                                            
Header2: 0400                                                                                                                                                                                                               
 index =  2 16bit chunk =  1024 checksum = 61222                                                                                                                                                                            
Header3: 0000                                                                                                                                                                                                               
 index =  3 16bit chunk =     0 checksum = 61222

当我比较每个步骤的输出时,都会看到以下内容:

C:

eb25
index:  0 chunk: 60197 checksum: 60197
0001
index:  1 chunk:     1 checksum: 60198
0000
index:  2 chunk:     0 checksum: 60198
0400
index:  3 chunk:  1024 checksum: 61222

Python:

{{1}}

0 个答案:

没有答案
相关问题