How to convert binary values within a list of ndarray objects to decimal values?

1

My list contains 49 entries. The size of the entries varies. For example, the first entry consists of 3032 rows and 27 columns. This corresponds to 27 signals with 3032 data each. The data is currently in binary. The size of the column also varies within an entry (the 27 columns do not have the same size). For example, the first signal of the first entry has the following format:

print(signal_reshape_list[0][:,0] #print first entry, first signal with all columns 

Output: [list([1, 1]) list([1, 1]) list([1, 1]) list([1, 1])....list([1, 1]) list([1, 1])]

list([1, 1]) corresponds to the binary number 0x00000011 and would therefore be "3" as decimal number representation.

print(signal_reshape_list[0][:,0] #print first entry, third signal with all columns 

Output: [list([0, 0, 1, 0]) list([1, 0, 1, 0]) list([0, 0, 1, 1]) .... list([0, 1, 1, 0]) list([0, 0, 1, 0]) list([0, 0, 1, 0])]

list([0, 0, 1, 0]) corresponds to the binary number 0x00000010 and would therefore be "2" as decimal number representation. list([1, 0, 1, 0]) corresponds to the binary number 0x00001010 and would therefore be "10" as decimal number representation. And so on...

python
binary
decimal
data-conversion

1 Answer

2

you have a list like this:

Output: [list([0, 0, 1, 0]) list([1, 0, 1, 0]) list([0, 0, 1, 1]) .... list([0, 1, 1, 0]) list([0, 0, 1, 0]) list([0, 0, 1, 0])]

and desire out put is like :[2,10,3,..,6,2,2] it can be down by below code that applied on output:

a= [list([0, 0, 1, 0]), list([1, 0, 1, 0]), list([0, 0, 1, 1])]
out=[]
for l in range(0,len(a)):
    A=a[l]
    out.append(A[3]+A[2]*2+A[1]*4+A[0]*8)

print(out)

And output: out: [2, 10, 3]

Update:

Each list elements should concatenate to get binary string like 0010then it will change to decimal value by int(result, 2) here is the code:

a= [list([0, 0, 1, 0]), list([1, 0, 1, 0]), list([0, 0, 1, 1,0, 0, 1, 1])]
out=[]
for l in range(len(a)):
    result= ''
    for element in a[l]:
        result += str(element)
    out.append(int(result,2))    

print(out)

Binary output:['0010', '1010', '00110011'] decimal output:[2, 10, 51]

answered on Stack Overflow Mar 15, 2020 by Mohsen • edited Mar 15, 2020 by Mohsen

User contributions licensed under CC BY-SA 3.0