-1

I need several k bits at position p extracted in order to convert to decimal value. I use a standard function but still when I test it for a 6/7/8 byte long binary code it is not correct. When I have a 1 byte code it is little-endian but once I use a 8 byte code it is shifted. For one signal (of a 7byte code) it was shifted +7bits but another signal (another ID, 8byte code) was shifted by -21bits. I cannot explain this to myself so I thought of playing around and manually add or subtract bits in order to use the correct bits for calculation. Do you have any idea why this is happening?

Example 8bytes:

extract_k_bits('100001111000000001110111011001000111011111111000001110100111101',16,0)

Output: 001110100111101 instead of 1000011110000000

extract_k_bits('100001111000000001110111011001000111011111111000001110100111101',16,24)

Output: 0110010001110111 instead of 1011001000111011

This is the code I am working with:

import openpyxl
from openpyxl import Workbook

theFile = openpyxl.load_workbook('Adapted_T013.xlsx')
allSheetNames = theFile.sheetnames 
print("All sheet names {} " .format(theFile.sheetnames)) 
sheet = theFile.active

def extract_k_bits(inputBIN,k,p):
        end = len(inputBIN) - p
        start = end - k + 1
        kBitSub = inputBIN[start : end+1]
        print(kBitSub)
        Dec_Values=int(kBitSub,2)
Nezuko
  • 34
  • 6
  • 2
    You are just slicing strings here, all the additional context about bits and bytes and signals is superfluous. Does this answer your question? https://stackoverflow.com/questions/509211/understanding-slice-notation – Hymns For Disco Feb 19 '20 at 06:44

1 Answers1

0

Here's a working solution:

def extract_k_bits(inputBIN,k): 
    # Since you always extract 16 bits
    # just use the point where you want to extract from
        kBitSub = inputBIN[k : k+16]
        print(kBitSub)

extract_k_bits('100001111000000001110111011001000111011111111000001110100111101',0)
extract_k_bits('100001111000000001110111011001000111011111111000001110100111101',23)

Output

Output image

APhillips
  • 1,175
  • 9
  • 17