Noob python inquiry

reef_d

Disciple
I'm trying out a very basic cryptography program by just shifting the mapping of characters. The user will enter a shift alphabet and i have to read from a plaintext file, shift each character by the shift value and write to a ciphertext file.

Eg. A - 0, B - 1, C - 2....Z - 25 and the same numerical values for the lower case alphabets.

So do i manually do the mapping in a dictionary or is there any ready made function that can help me do it. Also for the inverse mapping what would i have to do. Is there any method to do 0-A, 1-B after I have created the aforementioned dictionary?
 
you can use the ord function in python to get the ascii value and then you can subtract it with the ord values of A or a for the mapping. Basically it goes something like this :-

PHP:
ch = 'z'
c=ord(ch)
if c > 97:
c = c-ord('a')
else:
c = c - ord('A') + shift_no

this is the simplest I could think of or else you can use the hash mappings as sahilm suggested.
 
^^Much more elegant for this particular problem. Though hash tables will be better if he's looking to write scalabe code. If not, just doing the above is more than enough.
 
Yeah i have an idea about hashlib but I was just trying out simple stuff. Any helpful links on data structures and control structures would be appreciated.

@codereverser, thanks for the help. I had tried something like that as well.
 
Back
Top