modulo - Can someone explain to me how ( (c-65+k)%26)+65) works in a caesar cypher? -
if c numerical value of uppercase character (i.e. b 66) , sake of argument, k key value of 2? i'm new programming , don't understand how modulo works in this. know takes value of remainder, wouldn't simplify this?
c = b = 66 k = 2 imagine result should 'd' (66 - 65 +2)%26 +65 (3)%26 +65 0 + 65 65 = 'a'
i must not understand way % works.
key fact - ascii code of letter"a" 65.
here how cypher works - original expression in question title.
- take ascii value of letter, subtract value of "a" giving 0 based number.
- add key value number shifting
k
places. - now divide number got above 26, discard quotient , use remainder. modulo operator
%
. keeps numbers in 0-25 range, since dividing 26 never have remainder great 25. - add 65 convert "encrypted" uppercase letter.
this allows key number , still keeps "encrypted" output within ascii range of a-z.
you interpreting %
operator division. in reality, it's modulo or forget-the-quotient-i want-the-remainder operator.
example
0%2
01%2
12%2
03%2
1
and on. modulo cyclic.
Comments
Post a Comment