Issue
Write a script that decrypts a message coded by the method used in Project 6.
Method used in project 6:
Add 1 to each character’s numeric ASCII value. Convert it to a bit string. Shift the bits of this string one place to the left. A single-space character in the encrypted string separates the resulting bit strings.
An example of the program input and output is shown below:
Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101
Hello world!
The commented out stuff is seperate part of the code I'm not on. I am trying to get only with the binary to decimal conversion as I would like to figure as much out on my own as I can, but I feel like I'm getting too stuck and nothing I think of works. But in essence, it must be converted from binary to decimal, the decimal to ascii, then ascii to a writable string
This my code so far:
message = input("Enter the coded text: ")
decimal = 0
exponent = len(message) - 1
bString = ""
for digit in message:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
print(bString)
# for ch in value:
# value = chr(ch)
# bString = ""
# print(bString)
When I run this top half I get this in return
Traceback (most recent call last):
File "decrypt.py", line 6, in <module>
decimal = decimal + int(digit) * 2 ** exponent
ValueError: invalid literal for int() with base 10: ' '
Solution
version 4:
b = '0110001' # will store the binary string
i = int(b,2) # this will convert the binary to a decimal value
c = chr(i) # this will convert the decimal to ascii value
x = chr(i-1) # this will reduce 1 from the decimal and conver to ascii value
print (b, i, c, x) #print binary, decimal, ascii, and shifted ascii values
The output of this will be:
0110001 49 1 0
Another option based on what your question says:
b = '0110001' # will store the binary string
b = b + '1' # shifting the binary string one place to the left
i = int(b,2) # this will convert the new binary to a decimal value
c = chr(i) # this will convert the new decimal to ascii value
x = chr(i-1) # this will reduce 1 from the decimal and conver to ascii value
print (b, i, c, x) #print binary, decimal, ascii, and shifted ascii values
The output of this will be:
01100011 99 c b
I hope these inputs will help you get the best answer.
If you are able to clearly articulate your problem statement, I can help you solve the problem.
version 3: Binary to Decimal to ASCII conversion:
Here's a program that will convert the binary value to integer and then convert that to an ascii value for you to display.
message = input("Enter the coded text: ")
bString = ""
for digit in message.split():
bString += chr(int(digit,2))
print(bString)
The output of this program is as follows:
Enter the coded text: 01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
Hello, World!
version 2
If you are trying to convert the binary values into an integer, you can do it as simple as:
message = input("Enter the coded text: ")
for digit in message.split():
print (digit, int(digit,2))
The result of this will be:
Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101
0010011 19
1001101 77
1011011 91
1011011 91
1100001 97
000011 3
1110001 113
1100001 97
1100111 103
1011011 91
1001011 75
000101 5
However, this still does not give you Hello World. To give you Hello World, your input value has to be :
01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
Old answer
While this solves your ValueError, it does not solve your full problem.
I added a if statement inside your for-loop to convert only digits. To do that, i am check if the value is a digit using digit.isdigit()
. This will convert the value and compute into variable decimal (btw, its a bad variable name).
What is the use of bString. Do you intend to do something with it? You are printing out bString. What do you want to do with it?
message = input("Enter the coded text: ")
decimal = 0
exponent = len(message) - 1
bString = ""
for digit in message:
if digit.isdigit():
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
print(bString)
The output for this is as follows:
Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101
The output was a blank line as bString was empty string. However, i did check the value of decimal
. It printed out as follows:
>>> decimal
1493433473927764848841434501
Answered By - Joe Ferndz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.