Python Forum
Can someone please help me convert this simple C ROT cipher code to Python code? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can someone please help me convert this simple C ROT cipher code to Python code? (/thread-19117.html)



Can someone please help me convert this simple C ROT cipher code to Python code? - boohoo9 - Jun-13-2019

Hi!
I'm having difficulties converting this simple C code into Python code. I don't code in Python much, I'm only using it for Flask web application and I've been struggling to find help online, since all I can find is limited to alphabet shifting.

Basically, I need a code that shifts ASCII value of all characters in string array, no matter if they're valid alphabet letters.
So the C code looks like this:

[Sorry for using Python code formatting, I can't find an option for general code block]
#/* 'str[]' is character array, 'shift' is the number by which the ASCII value will be shifted */  
void ROTciph(char str[], int shift) {
    unsigned int i;
    for(i=0; i<strlen(str); ++i)
    {
          str[i] = str[i] + shift;
    }
}
I'd be very grateful if someone could help me 'translating' this into a Python code.
Thanks!

PS: I know this is a very beginner task, but time is playing against me, so once again, thanks for your help!


RE: Can someone please help me convert this simple C ROT cipher code to Python code? - micseydel - Jun-13-2019

This forum is focused on Python education, which means that we need to see an attempt from you first. Along with that, you should include a description of the specific problem which is blocking you from completing your task yourself.


RE: Can someone please help me convert this simple C ROT cipher code to Python code? - boohoo9 - Jun-13-2019

Sorry, I'll try someplace else. As I said, my specific problem is that my Python knowledge is close to absolute zero. Thanks and have a nice day!


RE: Can someone please help me convert this simple C ROT cipher code to Python code? - nilamo - Jun-13-2019

The first thing you should do is define some input/outputs. Some test strings, and what you expect the rotated values to be. Does "z" become 'm' or 'M', for example. What does whitespace rotate to? Digits?

From there, the string module is probably the next best place to look.
>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'



RE: Can someone please help me convert this simple C ROT cipher code to Python code? - boohoo9 - Jun-14-2019

(Jun-13-2019, 09:51 PM)nilamo Wrote: The first thing you should do is define some input/outputs. Some test strings, and what you expect the rotated values to be. Does "z" become 'm' or 'M', for example. What does whitespace rotate to? Digits? From there, the string module is probably the next best place to look.
>>> import string >>> dir(string) ['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace'] >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Thank you! I figured I just needed to shift ord() values of all characters in the given string and it works!

Thanks for your time!


RE: Can someone please help me convert this simple C ROT cipher code to Python code? - DeaD_EyE - Jun-14-2019

from collections import deque
import string

# original letters
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase

# copy of original letters as deque
lowercase_rot13 = deque(lowercase)
uppercase_rot13 = deque(uppercase)

# roate the deque by 13
lowercase_rot13.rotate(13)
uppercase_rot13.rotate(13)

# combine the lowercase and uppercase
charset = lowercase + uppercase
charset_rot13 = (lowercase_rot13 + uppercase_rot13)

# use join, to convert the deque back to a str
charset_rot13 = ''.join(charset_rot13)

# make the translation
encode_rot13 = str.maketrans(charset, charset_rot13)
decode_rot13 = str.maketrans(charset_rot13, charset)

# test
msg = 'Hello World'
enc = msg.translate(encode_rot13)
dec = enc.translate(decode_rot13)
print(msg)
print(enc)
print(dec)
The encode_rot13 object is a dict.
The method str.translate uses this dict.
And sorted:
We can make this dict with one line:
encode_rot13 = {char: char + 13 for char in range(65, 122 + 1)}
decode_rot13 = {v: k for k, v in encode_rot13.items()} # just swapping keys with values