Convert decimal to binary - python

Asked

Viewed 2,710 times

2

Hello guys I’m new in python my code below displays the values of the ASCII table for a typed string. How can I take these values and convert to binary

valor = input("Insira o valor a ser criptografado: ")
print(valor) 
list=[ord(ch) for ch in valor]
print("Valores da tabela ASCII para:", valor, "são:", list, ".")

result (ENTERED VALUE = test): Values of the ASCII table for (ENTERED VALUE) are: [84, 69, 83, 84, 69] .

Now I need to get 84 >> pass to binary and so on

why later I will make a function with a key and encrypt the values etc.

  • 3

    It’s not just doing bin(84)?

  • I do not know how to answer you friend, so I asked, but researching your tip.. worked very well. Thank you!

  • @Woss puts his suggestion as an answer so he can choose the correct answer and close the question. Or else Leomouraiot put what he did to resolve and mark as correct answer to close the question.

2 answers

6

The "bin" function gives the binary representation of a number as a string. Mse this representation as string is useful only to show the "0" and "1" on the screen, to be seen by people. Internally, the algorithms that work with the letters, be it encryption, or others, have to use the numbers - a number does not have a "base" it is a quantity - the representation of it as text will happen naturally in decimal, and the number representation for algorithms that manipulate bit by bit numbers is already in binary.

What you need, yes, is to convert your text to an encoding in Bytes. If you only use "Ord", you have Codepoint Unicode representing your character, but that number - Codepoint- does not have a fixed width, and then most cryptographic algorithms will not work (you can take each Codepoint as a 32 bit width, the equivalent of using utf-32, but it will take up space).

In short, if you want to work with text with a sequence of numbers, it is best to transform it into an object of type "bytes", using a suitable encoding (for example, "utf-8", which covers all known characters). An object "bytes" if you access each element, already gives you the number, and not an object bytes of length "1":

In [138]: for x in "maçã".encode("utf-8"):
     ...:     print(x, end=", ")
     ...: 
109, 97, 195, 167, 195, 163, 

Already, going back to the original question, if you want, for the purpose of visualisation, see the binary representation of the numbers, just use the function "bin":

In [140]: for x in "maçã".encode("utf-8"):
     ...:     print(x, bin(x), sep="\t")
     ...: 
109     0b1101101
97      0b1100001
195     0b11000011
167     0b10100111
195     0b11000011
163     0b10100011
  • 1

    this coding business is IMPORTANT to understand, regardless of whether you want to work with cryptography, etc... I recommend reading this article very carefully here: https://papacharliefox3.wordpress.com/2009/03/28/o-minimum-knowledgede-software-should-know-no-shadow-of-doubt-on-one-and-haracter-sets-no-apologies-tattered/

4


I know the previous answer already answers sufficiently the question. However, I solved the issue with a new outfit. The code is:

valor = input('Insira um valor a ser criptografado: ')
for c in valor:
    v = ord(c)
    print(c, v, f'{v:b}', sep='\t')

Note that in this code I did not use the function bin(). In this code I used f-string formatting to binary. This way we can control the prefix display "0b".

The part of the code that actually converts a value to binary is:

f'{v:b}'

Using this notation the binary value will be displayed without the prefix 0b.

Now, if we prefer to display the prefix 0b, just use the character "#" immediately before the letter "b". In this case the code would be:

f'{v:#b}'

If all binary values are shown with 8 digits, we can use the following format:

f'{v:08b}'

In this way, all binary values will have 8 digits.

  • 1

    to exercise the f-strings, you can take and put an example of how to guarantee 8 binary digits - if you look at the example of my answer, the letters "ma" have only 7 digits - you would have 0 in front so that all have the same width.

  • 1

    @jsbueno, binary value formatting with 8 dígitos, successfully performed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.