PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

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

 August 11, 2022     binary, data-conversion, decimal, python     No comments   

Issue

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...


Solution

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 By - Mohsen
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing