[Tutor] binary translator
Tiger12506
keridee at jayco.net
Sun Dec 16 00:39:30 CET 2007
>
> Hey i have created a program that turns a string into a binary one. But
> when i began to test the program it turned out that it could not handle
> some special characters (e.g ÆØÅ). Could someone please help me?
These special characters have different values than those you have put into
your big list. I have suggestions down below.
>
> p.s i am a newbie so if you have any comments on the source code please
> let me know
I have a couple.
> here is the source code:
>
> #!/usr/bin/python
> # -*- coding: Latin-1 -*-
> import os, sys
> import sys
>
> def ChooSe():
> print
> print " encrypt "
> print " quit "
> print
You know that you can make this one print statement if you wish? ( \n is the
escape character for a new-line)
print "\nencrypt\nquit"
or more easily readable~
print """\
encrypt
quit
"""
> choice = raw_input("What do you need: ")
>
> if choice == "encrypt":
> encrypt()
> elif choice == "quit":
> kExit()
> else:
> print "Invalid choice"
> ChooSe()
> def kExit():
> xit = raw_input("Do you really want to quit? ")
> kY = ('y','Y','yes','yah','yeah')
> kN = ('n','N','no','nah')
You could put those tuples directly into the if statement, it is not
necessary to assign them to a variable first. I think it makes it more
readable. You do not have to.
if xit in ('n','N','no','nah'):
> if xit in kN:
> ChooSe()
> elif xit in kY:
> print "Goodbye!"
> print
> sys.exit()
> else:
> print "Invalid choice"
> ChooSe()
> def encrypt():
>
> print
> print " encryption "
> print
>
> secret = raw_input('Text to encrypt: ')
> code = list(secret)
^^^^^^^^^^^^^^^^^
This is unnecessary. secret is already iterable.
secret[0] == code[0]
>
> chartobinary = {
> 'A' : '01000001',
> 'B' : '01000010',
> 'C' : '01000011',
> 'D' : '01000100',
> 'E' : '01000101',
> 'F' : '01000110',
> 'G' : '01000111',
> 'H' : '01001000',
> 'I' : '01001001',
> 'J' : '01001010',
> 'K' : '01001011',
> 'L' : '01001100',
> 'M' : '01001101',
> 'N' : '01001110',
> 'O' : '01001111',
> 'P' : '01010000',
> 'Q' : '01010001',
> 'R' : '01010010',
> 'S' : '01010011',
> 'T' : '01010100',
> 'U' : '01010101',
> 'V' : '01010110',
> 'W' : '01010111',
> 'X' : '01011000',
> 'Y' : '01011001',
> 'Z' : '01011010',
> 'a' : '01100001',
> 'b' : '01100010',
> 'c' : '01100011',
> 'd' : '01100100',
> 'e' : '01100101',
> 'f' : '01100110',
> 'g' : '01100111',
> 'h' : '01101000',
> 'i' : '01101001',
> 'j' : '01101010',
> 'k' : '01101011',
> 'l' : '01101100',
> 'm' : '01101101',
> 'n' : '01101110',
> 'o' : '01101111',
> 'p' : '01110000',
> 'q' : '01110001',
> 'r' : '01110010',
> 's' : '01110011',
> 't' : '01110100',
> 'u' : '01110101',
> 'v' : '01110110',
> 'w' : '01110111',
> 'x' : '01111000',
> 'y' : '01111001',
> 'z' : '01111010',
> ' ' : '00100000',
> '!' : '00100001',
> "\\" : '00100010',
> '#' : '00100011',
> '$' : '00100100',
> '%' : '00100101',
> '&' : '00100110',
> "'" : '00100111',
> '(' : '00101000',
> ')' : '00101001',
> '*' : '00101010',
> '+' : '00101011',
> ',' : '00101100',
> '-' : '00101101',
> '.' : '00101110',
> '/' : '00101111',
> ':' : '00111010',
> ';' : '00111011',
> '<' : '00111100',
> '=' : '00111101',
> '>' : '00111110',
> '?' : '00111111',
> '@' : '01000000',
> '[' : '01011011',
> ']' : '01011101',
> '^' : '01011110',
> '_' : '01011111',
> '`' : '01100000',
> '{' : '01111011',
> '|' : '01111100',
> '}' : '01111101',
> '~' : '01111110',
> '€' : '10000000',
> '¡' : '10100001',
> '¢' : '10100010',
> '£' : '10100011',
> '¤' : '10100100',
> '¥' : '10100101',
> '¦' : '10100110',
> '§' : '10100111',
> '¨' : '10101000',
> '©' : '10101001',
> 'ª' : '10101010',
> '«' : '10101011',
> '¬' : '10101100',
> '®' : '10101110',
> '¯' : '10101111',
> '°' : '10110000',
> '±' : '10110001',
> '²' : '10110010',
> '³' : '10110011',
> '´' : '10110100',
> 'µ' : '10110101',
> '¶' : '10110110',
> '·' : '10110111',
> '¸' : '10111000',
> '¹' : '10111001',
> 'º' : '10111010',
> '»' : '10111011',
> '¼' : '10111100',
> '½' : '10111101',
> '¾' : '01111110',
> '¿' : '10111111',
> 'À' : '11000000',
> 'Á' : '11000001',
> 'Â' : '11000010',
> 'Ã' : '11000011',
> 'Ä' : '11000100',
> 'Å' : '11000101',
> 'Æ' : '11000110',
> 'Ç' : '11000111',
> 'È' : '11001000',
> 'É' : '11001001',
> 'Ê' : '11001010',
> 'Ë' : '11001011',
> 'Ì' : '11001100',
> 'Í' : '11001101',
> 'Î' : '11001110',
> 'Ï' : '11001111',
> 'Ð' : '11010000',
> 'Ñ' : '11010001',
> 'Ò' : '11010010',
> 'Ó' : '11010011',
> 'Ô' : '11010100',
> 'Õ' : '11010101',
> 'Ö' : '11010110',
> '×' : '11010111',
> 'Ø' : '11011000',
> 'Ù' : '11011001',
> 'Ú' : '11011010',
> 'Û' : '11011011',
> 'Ü' : '11011100',
> 'Ý' : '11011101',
> 'Þ' : '11011110',
> 'ß' : '11011111',
> 'à' : '11100000',
> 'á' : '11100001',
> 'â' : '11100010',
> 'ã' : '11100011',
> 'ä' : '11100100',
> 'å' : '11100101',
> 'æ' : '11100110',
> 'ç' : '11100111',
> 'è' : '11101000',
> 'é' : '11101001',
> 'ê' : '11101010',
> 'ë' : '11101011',
> 'ì' : '11101100',
> 'í' : '11101101',
> 'î' : '11101110',
> 'ï' : '11101111',
> 'ð' : '11110000',
> 'ñ' : '11110001',
> 'ò' : '11110010',
> 'ó' : '11110011',
> 'ô' : '11110100',
> 'õ' : '11110101',
> 'ö' : '11110110',
> '÷' : '11110111',
> 'ø' : '11111000',
> 'ù' : '11111001',
> 'ú' : '11111010',
> 'û' : '11111011',
> 'û' : '11111100',
> 'ý' : '11111101',
> 'þ' : '11111110',
> 'ÿ' : '11111111',
> '0' : '00110000',
> '1' : '00110001',
> '2' : '00110010',
> '3' : '00110011',
> '4' : '00110100',
> '5' : '00110101',
> '6' : '00110110',
> '7' : '00110111',
> '8' : '00111000',
> '9' : '00111001',
> }
Holy cow! I can't believe you went to all of this work! Why write a program?
Why not just do it all by hand?
If you can think of no other way to do it, I suggest that you look at the
documentation for this function
ord() which stands for 'ordinal'
and also do a search for a function that converts an integer to a binary
string representation.
> def TransLate():
> key = 1
> fTransLaTion = chartobinary[code[0]]
> aTransLaTion = ""
> while key <= len(code) - 1:
> TransLaTion = chartobinary[code[key]]
> key = key + 1
> aTransLaTion = aTransLaTion+TransLaTion
> lTranslation = fTransLaTion+aTransLaTion
> print lTranslation
> TransLate()
> ChooSe()
> ChooSe()
A general comment: I DoN't lIkE tHe mIxeD CaSe YoU hAvE iN YouR COdE, AnD
i'M sURe oThERs wIlL AgrEe ThAT iT mAKeS iT LeSs ReADabLe.
Okay I have to write this down so that I can follow the code in translate.
1) set key = 1, set ftrans to the binary of the first character
2) establish atrans as a string
3) loop from the second character all the way until the last using key as an
incrementor
4) set trans to the binary string of the character
5) add this character to the string atrans
6) add this whole string to the first string ftrans, then set to
ltrans
How about:
def translate():
trans = []
for c in secret:
trans.append(chartobinary[c])
return "".join(trans)
First, establishes trans as a list. Then sets up a loop so that each time
through, the variable 'c' is set to the next character in the *string*
secret. (Or the list code, but as I said above, this is unnecessary). Each
time through the loop I append the binary string of c to the end of the list
trans. After the loop, I take every element of the list trans and combine
them directly together into one string with this -> "".join(trans)
More information about the Tutor
mailing list