Python Forum
How to assigned value to each different binary in python...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to assigned value to each different binary in python...
#1
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

Attached Files

Thumbnail(s)
   
Reply
#2
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
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 591 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 6,460 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  How to convert a python code to binary? rsurathu 0 1,793 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  python read binary file Pyguys 4 3,860 Jul-13-2020, 02:34 AM
Last Post: Pyguys
  Simple text to binary python script gmills13 2 2,806 Feb-04-2020, 08:44 PM
Last Post: snippsat
  hex file to binary or pcap to binary baran01 1 5,682 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Function assigned at a button in tkinter riccardoob 9 4,156 Oct-06-2019, 11:14 AM
Last Post: riccardoob
  assigned variable fails to show up Skaperen 4 2,355 May-27-2019, 10:48 AM
Last Post: Skaperen
  Python script that calls jamfHelper binary kennethdean2010 19 7,118 May-07-2019, 03:50 PM
Last Post: kennethdean2010

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020