Python Forum

Full Version: How to assigned value to each different binary in python...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
this is my code for converting hex to binary
I converted 6EDAC

# conversion of hex string
# to binary string

import math

# Initialising hex string
ini_string = "6EDAC"

# Printing initial string
print ("Initial string", ini_string)

# Code to convert hex to binary
res = "{0:08b}".format(int(ini_string, 16))

# Print the resultant string
print ("Resultant string", str(res))

so I get 1101110110110101100 in Bin as a result!
from this bin i want to assign a specified value to each and every bits.
and from there i will add all the 1's value in sum and that's what i want to achieve.
is this possible? im stuck here :( help please
Please state the purpose of the statement, and show what you have tried, and where you think you are making a mistake.
Please read: https://python-forum.io/misc.php?action=help&hid=52
You could perhaps use numpy for this
>>> import numpy as np
>>> a = np.array([4, 3, 7, 2, 1, 8, 9])
>>> b = np.array([0, 1, 1, 1, 0, 0, 1], dtype=bool)
>>> np.sum(a, where=b)
21