1

So I have a code that I am working on. I have run into a problem where I cannot figure out a way to convert a list I have in decimal to binary. I have searched for examples, but cannot find a solution. If you could help me out with a way to handle this way specifically that would be great thanks.

Code

PT = input("Please enter your plaintext: ")
PT = PT.lower()
K = input("Please enter your key: ")
K = K.lower()
output1 = []
output2 = []
for character in PT:
    if character.isdigit():
        digit = ord(character) - 22
        output1.append(digit)
    elif character.isalpha():
        number = ord(character) - 97
        output1.append(number)
    else:
        print("Please use letters and numbers only. (Plaintext)")
        break

for character in K:
    if character.isdigit():
        digit = ord(character) - 22
        output2.append(digit)
    elif character.isalpha():
        number = ord(character) - 97
        output2.append(number)
    else:
        print("Please use letters and numbers only. (Key)")
        break
Boooo402
  • 27
  • 1
  • 9
  • Took multiple times to fix the indent issues. – Sphinx Jan 25 '18 at 01:47
  • You should use raw_input instead of input. [The differences between input and raw_input](https://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) – Sphinx Jan 25 '18 at 01:52
  • I'm actually on python 3.5.0 so wouldn't raw_input just be input now? or am I mistaken @Sphinx – Boooo402 Jan 25 '18 at 01:54
  • In python 3, they are same. What is the problem you met? Can you provide more details? – Sphinx Jan 25 '18 at 02:01
  • This is what I received when I replaced input with raw_input ------------------------------------------------------------------line 1, in PT = raw_input("Please enter your plaintext: ") NameError: name 'raw_input' is not defined – Boooo402 Jan 25 '18 at 02:03
  • May bad because I am running on 2.7. But I am still confuse what problem you met. – Sphinx Jan 25 '18 at 02:07
  • When I run my program it outputs in a list the ascii values of the string input. It will subtract 22 or 97 if its a number or letter respectfully. I want to know how to convert that ascii value from its current state (decimal) to binary. – Boooo402 Jan 25 '18 at 02:11
  • Try use bin(number/char). – Sphinx Jan 25 '18 at 17:59

1 Answers1

0

You can use bin() to convert the object to the binary.

PT = raw_input("Please enter your plaintext: ")
PT = PT.lower()
K = raw_input("Please enter your key: ")
output1 = []
output2 = []
for character in PT:
    if character.isdigit():
        digit = ord(character) - 22
        output1.append(bin(digit))
    elif character.isalpha():
        number = ord(character) - 97
        output1.append(bin(number))
    else:
        print("Please use letters and numbers only. (Plaintext)")
        break
for character in K:
    if character.isdigit():
        digit = ord(character) - 22
        output2.append(bin(digit))
    elif character.isalpha():
        number = ord(character) - 97
        output2.append(bin(number))
    else:
        print("Please use letters and numbers only. (Key)")
        break
print output1
print output2

Test case:

Please enter your plaintext: abc

Please enter your key: 234

['0b0', '0b1', '0b10']

['0b11100', '0b11101', '0b11110']

Sphinx
  • 10,519
  • 2
  • 27
  • 45