| | |
Encoding/Decoding
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
Hey everyone. Been doing C for a little while now and this website has helped me in advancing my knowledge and skills in C programming.
I have another question now... it's really bugging me actually... I'm trying to make a program basically just encode and decode. It seems too simple for me not to understand but... I'm teaching myself so that's probably the problem.
I'm trying to get something like this...
First Sentence: See Spot Run
Second Sentence: FSS FIJW BNK
but then I also want to output the opposite...
so if I enter
First Sentence: FSS FIJW BNK
I'll get
Second Sentence: See Spot Run or (SEE SPOT RUN, whichever)
If anyone could push me in the right direction or have any programs they could show me that'd be great. Then I can build on it from there!
I have another question now... it's really bugging me actually... I'm trying to make a program basically just encode and decode. It seems too simple for me not to understand but... I'm teaching myself so that's probably the problem.
I'm trying to get something like this...
First Sentence: See Spot Run
Second Sentence: FSS FIJW BNK
but then I also want to output the opposite...
so if I enter
First Sentence: FSS FIJW BNK
I'll get
Second Sentence: See Spot Run or (SEE SPOT RUN, whichever)
If anyone could push me in the right direction or have any programs they could show me that'd be great. Then I can build on it from there!
Consider recursion to "get to the bottom" before starting.
C Syntax (Toggle Plain Text)
#include <stdio.h> void foo(FILE *file) { char line [ 128 ], *ch; if ( fgets(line, sizeof line, file) ) { foo(file); /* * Reached bottom. */ fputs(line, stdout); } }
One of the ways to encode and decode using the same function is with a bitwise xor. Now you are making it more difficult demanding that the encoded sentence be in characters that are on the keyboard. This little code sample does it using a set pass-key and an exception handler.
[php]
// this simple xor crypt may fool mom, but not the FBI
// all encoded characters should be on the keyboard
// Pelles C
// VPW = 4 only gives one non-key char
#define VPW 4
#define EOS 0
#include <stdio.h>
#include <string.h>
char *crypt(char *str);
int main()
{
static char str[80];
while(1)
{
printf("Enter a string (nothing to exit): ");
gets(str);
if (strlen(str) == 0)
break;
printf("Original : %s\n",str);
printf("Encrypted : %s\n",crypt(str));
}
getchar(); // wait
return 0;
}
char *crypt(char *str)
{
int k, n;
char c1, c2;
char cpw; // password char
static char temps[80]; // hold the crypted string
for (k = 0, n = 0; k < strlen(str); k++, n++)
{
c1 = str[k];
cpw = VPW;
// ^ is the bitwise xor operator
c2 = (c1 ^ cpw);
// keep the spaces
if (c1 == ' ')
c2 = ' ';
// FOR VPW = 4 one exception needed, d gives non-key char
if (c1 == 'd')
c2 = '>';
if (c1 == '>')
c2 = 'd';
temps[k] = c2; // build the temporary string
}
temps[k++] = EOS; // end it correctly
return temps;
}
[/php]
[php]
// this simple xor crypt may fool mom, but not the FBI
// all encoded characters should be on the keyboard
// Pelles C
// VPW = 4 only gives one non-key char
#define VPW 4
#define EOS 0
#include <stdio.h>
#include <string.h>
char *crypt(char *str);
int main()
{
static char str[80];
while(1)
{
printf("Enter a string (nothing to exit): ");
gets(str);
if (strlen(str) == 0)
break;
printf("Original : %s\n",str);
printf("Encrypted : %s\n",crypt(str));
}
getchar(); // wait
return 0;
}
char *crypt(char *str)
{
int k, n;
char c1, c2;
char cpw; // password char
static char temps[80]; // hold the crypted string
for (k = 0, n = 0; k < strlen(str); k++, n++)
{
c1 = str[k];
cpw = VPW;
// ^ is the bitwise xor operator
c2 = (c1 ^ cpw);
// keep the spaces
if (c1 == ' ')
c2 = ' ';
// FOR VPW = 4 one exception needed, d gives non-key char
if (c1 == 'd')
c2 = '>';
if (c1 == '>')
c2 = 'd';
temps[k] = c2; // build the temporary string
}
temps[k++] = EOS; // end it correctly
return temps;
}
[/php]
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
I appologize but I really do not understand this code you've given me. I just know basic C and "crypt" and "VPW" and "EOS" all make no sense to me. Is there anyway you could show me a more basic code? That way once I understand the basic code it will allow me to understand and make more advanced programs. Thank you.
•
•
•
•
Originally Posted by tyalangan
I appologize but I really do not understand this code you've given me. I just know basic C and "crypt" and "VPW" and "EOS" all make no sense to me. Is there anyway you could show me a more basic code? That way once I understand the basic code it will allow me to understand and make more advanced programs. Thank you.
For a good intro to C see:
http://gd.tuwien.ac.at/languages/c/p...own/cstart.htm
May 'the Google' be with you!
![]() |
Similar Threads
- help with decoding a cipher (Python)
- encoding/decoding (C)
- Encoding and decoding. basic C (C)
- Encoding and decoding with Magnetic Card Writer / Reader (Visual Basic 4 / 5 / 6)
- A question about SCASB, using it in decoding. (Assembly)
- Having trouble ENCODING/DECODING ASSEMBLY (Assembly)
- Qestion on Encoding and Decoding. (Python)
Other Threads in the C Forum
- Previous Thread: looping and spaces
- Next Thread: memory allocation
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab windows.h






