Can someone please break down this code for me? I know it changes text from a user file and I know it could be very useful to me. What is the purpose of “~”? How could I amend this code to read a user file word by word and then change it using the same kind of formula?
// first value in the file is the key
if ( fread(&key, sizeof(char), 1, infile) )
{
key = ~key;
}
while( fread(&fval ,sizeof(short), 1, infile) )
{
fputc( (fval / 2) - key, outfile );
}
key = ~keyswaps all the bits of keyYou know about bits?
ascii
A(65) is 100 0001 in binary, so ‘~’ of this is simply swaps each1for0and each0for1giving 011 1110 (62) which is>So this will replace all the
As in your document with>and similarly for every other character. The nice thing about ~ is that it’s exactly the same process to decrypt – just swap each bit back.ps. It’s not exactly mil-spec encryption!