Click here to Skip to main content
15,849,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert an array of binary integers to two bit signed little endian format. I have written the below function. But not getting the o/p. Where am I going wrong? Please help.
i/p: data_array = [1100, 10111, 100010, 101101, 111000, 1000011, 1001110, 1011001, 1100010, 1010111, 1001100, 1000001, 110110, 101011, 100000, 10101, 1011]

What I have tried:

def as_signed_little(data_array):
    as_bytes = int((data_array, 2)).to_bytes(2, byteorder='little', signed=True)
    return as_bytes 
Updated 26-Feb-22 4:49am

1 solution

I'm not at all sure what you expect that to do (or any idea what you actually get) but you have to realize that the number that you input is almost certainly stored in little endian format anyway: least significant byte of the data in the lowest memory address, and even within each byte, the least significant bit is stored in the lowest numbered bit.

And since none of your data appears to be "real binary" data but binary values expressed in decimal numbers (you have to prefix each with "0b" to get binary literal values:
[0b1100, 0b1011, ...
what is actually stored in your input array would be
[0b10001001100, 0b10011101111111, ...

I think you will need to be a lot more specific in exactly what you are inputting, and exactly what you expect to get as an output before you go any further, and then think about exactly how you would check if it is working before you start coding: it's very unlikely that there is any "standard function" which will do exactly what you are asking for!
Share this answer
Stefia David 26-Feb-22 1:51am    
Thank you so much. I will check the same. I too have got a vague idea about this from my colleague and have been trying since two days and reached until here. I am looking for a solution something similar like below.
input_data = [['1100110111111011','1101111011111111','0010101000000011'],['1100111111111011','1101100111111111','0010110100000011']]
Desired Output =[[-1074, -34, 810],[-1703, -39, 813]]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900