Python Forum
How to convert binary data into text?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert binary data into text?
#1
hi

this is my coding to convert HEX to Binary :
# 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))
and the results I've got is 1101110110110101100 in binary, i would like to covert them into text in python,
like a text "1101110110110101100" so that i can assign each and give every single one of them a declaration like this
binary position 19 = 0.5
binary position 18 = 0.25 ... etc
in the end i want to do a simple check and add function :
if binary 19 = 1, binary 18 =1, 0.5 + 0.25 etc..
the results will be something like 0.0011.

anyone got an idea how to convert or assign each binary position a value?
appreciate your help here :)
Larz60+ write Jul-16-2021, 04:37 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
try:
# conversion of hex string
# to binary string
import math

# Initialising hex string
ini_string = "6EDAC"
res = bin(int(ini_string, 16))[2:].zfill(8)
print(res, type(res))

# each bit can be addressed like res[0], res[6], etc.
print(res[0])
print(res[6])
this gives:
Output:
1101110110110101100 <class 'str'> 1 0
Reply
#3
String-formatting could help.


important_value = 42

print(f"{important_value:08b}")
print(f"{important_value:016b}")
print(f"{important_value:024b}")
print(f"{important_value:032b}")
Quote:00101010
0000000000101010
000000000000000000101010
00000000000000000000000000101010
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Does this do what you want?
# weight for bit 0, 1, ... 7
weights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.4, 0.3, 0.2]
number = 0x2C
# Convert number to binary '0b101100', remove '0b' and reverse order of bits to '001101'
bits = bin(number)[:1:-1] 

# If bit is set, put corresponding weight in values list, else 0
values = [weight if bit=='1' else 0 for weight, bit in zip(weights, bits)]
print(values, sum(values))
You can replace the string conversion stuff with math
values = []
for weight in weights:
    values.append(weight * number % 2)
    number = number // 2
print(values, sum(values))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert File to Data URL michaelnicol 3 1,170 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  openpyxl convert data to float jacklee26 13 5,996 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  Convert nested sample json api data into csv in python shantanu97 3 2,838 May-21-2022, 01:30 PM
Last Post: deanhystad
  Separate text files and convert into csv marfer 6 2,880 Dec-10-2021, 12:09 PM
Last Post: marfer
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,783 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  how to create a tool with tkinter to convert img to text rachidel07 3 2,584 Feb-05-2021, 12:21 PM
Last Post: deanhystad
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,356 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Convert file of hex strings to binary file medatib531 4 13,749 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  python3 convert hex to binary 32 bit Mkt 3 3,953 Aug-28-2020, 02:34 PM
Last Post: Mkt
  Binary data to Image convert Nuwan16 1 5,681 Aug-24-2020, 06:03 AM
Last Post: millpond

Forum Jump:

User Panel Messages

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