Concatenate numbers in binary [duplicate]

2024/11/16 0:24:05

When converting a number in binary in Python what you get is the following:

b = bin(77)
print(b)  # 0b1001101

When I was expecting 01001101. I am guessing that the b is there to let Python know that this is a binary number and not some integer. And that is ok within Python but what is considered safe practise here if you want to communicate with the outside world? This might be a silly example but online converters for instance do not recognise the above binary.

Would simply removing b always do the trick? Because I seem to be running into problems trying to code the Ascii85 encoder/decoder where concatenations of binary numbers take place. You can take a look at this example here.

My code is this case produces the following:

ch = 'Man '
list_ = [ord(x) for x in ch]  # [77, 97, 110, 32]
binary_repr = ''.join(bin(x) for x in list_)  # 0b10011010b11000010b11011100b100000
# When it should be                                01001101011000010110111000100000

Notice that simply replacing the b with nothing doesn't quite cut it here. This is probably some dumm mistake but can someone clear things up for me?

Answer
>>> format(b, '08b') 

Where b is your number and '08b' is the number of bit you want to use representing your number, if the parameter is #08b instead of 08b, you get the 0b in front of the number.

use format in every further operation and you should be good!

https://en.xdnf.cn/q/119253.html

Related Q&A

AttributeError: DataFrame object has no attribute path

Im trying incrementally to build a financial statement database. The first steps center around collecting 10-Ks from the SECs EDGAR database. I have code for pulling the relevant 8-Ks, 10-Ks, and 10-Qs…

two DataFrame plot in a single plot matplotlip

I want to plot two DataFrame in a single plot.Though, I have seen similar post but none seems to work out. First 5 rows of my dataframe looks like this: df1name type start stop stran…

Automate `lxc-attach` through ssh with Python

Question: How do I automate this process, and include all of the password prompts? Machine 1> ssh user2@machine2password: Machine 2> lxc-attach -n 0x1000 Container> ssh user3@machine3password…

Value of a key in a Python dictinary is not updating [duplicate]

This question already has answers here:Appending a dictionary to a list - I see a pointer like behavior(3 answers)Python The appended element in the list changes as its original variable changes(1 answ…

Python - __init__() missing 1 required positional argument:

Im kinda new to python and I cant get past this error: Traceback (most recent call last):File "***", line 63, in <module>bst = Node() TypeError: __init__() missing 1 required positional…

discord py - Custom command prefix doesnt work (no command run)

i have a problem that i cant solve. Im trying to add a prefix switcher for all guilds, that uses my bot. So Ive done that, but currently no command gets triggered and I cant find a solution since hours…

How to use sep parameter in .format?

I just started learning python and Im experimenting new things. isim = input("Name:") soyad = input("Surname:") yaş = input("Age:") edu = input("Education:") ge…

Python table classification

I have different type of data for example:4.5,3.5,U1 4.5,10.5,U2 4.5,6,U1 3.5,10.5,U2 3.5,10.5,U2 5,7,U1 7,6.5,U1I need output:U1: [[4.5, 3.5], [4.5, 6], [5, 7], [7, 6.5]] U2: [[4.5, 10.5], [3.5, 10.5]…

python json.loads / json.load truncates nested json objects?

given the following code: import json foo = {"root":"cfb-score","children":{"gamecode":{"attribute":"global-id"},"gamestate":{&quo…

How to make an encrypted executable file

I have made a tool/program on Ubuntu written in Python. I want to give this to my friend to test on his PC, but I dont want to share the source code.This program has many folders and many .py files. Is…