Re: Mathematics of the Enigma cipher?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sat, 30 Dec 2006 13:37:21 -0500
Michael Olea wrote:
CBFalconer wrote:
DarkProtoman wrote:
... snip ...
{
memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",36);
}
int GetCurrentPos(){return CurPos;}
int CharacterMap(char Char)
{
if(Char='A')
return 0;
else if(Char='B')
return 1;
... snip ...
else if(Char='7')
return 34;
else if(Char='8')
return 35;
else if(Char='9')
return 36;
}
why all this code? Try (in C):
static const char *alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const char *p;
if (p = strchr(alphabet, char)) return (p - alphabet) + 1;
else return 0; /* detect not in alphabet */
with which you can easily modify the alphabet in one place. Hint:
eschew code that requires various areas to agree if any change is
desired.
There is no need to store a string or loop over it, since in ASCII
uppercase alpha is contiguous, as are digits. So a version that
checks for invalid input:
Just as all computers are not x86s, all char sets are not ASCII.
Do you want your code to work, or do you want it to fail
mysteriously?
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
.
- Follow-Ups:
- Re: Mathematics of the Enigma cipher?
- From: DarkProtoman
- Re: Mathematics of the Enigma cipher?
- Prev by Date: Re: How to program an enigma cipher?
- Next by Date: Re: Mathematics of the Enigma cipher?
- Previous by thread: Re: Mathematics of the Enigma cipher?
- Next by thread: Re: Mathematics of the Enigma cipher?
- Index(es):
Relevant Pages
|