Thread: Encoding with random ASCII

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Unhappy Encoding with random ASCII

    I have to creat a program:

    1.enter a line of text and convert to ASCII
    2.encode the text by adding a random nunber to the ASCII char.


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>


    unsigned int a[80],ran,n=0,t,len=0,asc[80],n1=0,n2=127;
    char input[80],ch;

    void main(){

    scanf("%s",&input); //receive data input
    len=strlen(input)

    for(int i=0;i<len;i++){
    asc[i]=input[i];
    asc[i]+=ran;

    }
    (then print it by for loop)
    }

    how can i made a different random no. whenever i run it?
    and space can't be converted in ASCII when printing while i input a space in text.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    I didn't understood you...

    If the problem is generating random numbers, you should look at this simple code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int num_ran(int max)
    {
        int num;
    
        randomize();
        num = rand() % max;
    
        return num;
    
    }
    
    int main()
    {
        int rand_num;
    
        rand_num = num_ran(30);
        printf("The random number was: %d\n",rand_num);
    
        return 0;
    }

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: I didn't understood you...

    Originally posted by Scripter
    If the problem is generating random numbers, you should look at this simple code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int num_ran(int max)
    {
        int num;
    
        randomize();
        num = rand() % max;
    
        return num;
    
    }
    
    int main()
    {
        int rand_num;
    
        rand_num = num_ran(30);
        printf("The random number was: %d\n",rand_num);
    
        return 0;
    }
    problems:

    includes conio.h, a nonstandard header, for no good reason.
    uses randomize(), a nonstandard function.
    uses the "rand() % n" method to generate random numbers in a certain range which can be quite inaccurate
    hello, internet!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code here (see my signature for an example).

    >>void main
    ... is wrong, its
    >>int main(void)
    ...and return 0; at the end.

    >>1.enter a line of text and convert to ASCII
    What are you reading it as then? EBCDIC??! Are you doing this on a mainframe/AS400 or something?

    >>2.encode the text by adding a random nunber to the ASCII char.
    If you don't remember this random number, how will you decrypt your data?

    >>how can i made a different random no. whenever i run it?
    Look up the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Actually the procedures are:

    1.
    Convert each character, including blank space, to its ASCII equivalent

    2.
    Generate a positive random integer. Add this integer to the ASCII equivalent of each character. The same random integer will be used for the entire line of text.

    3

    Suppose that N1 represents the lowest permissible value in the ASCII code, and N2 represents the highest permissible value.

    If the number obtained in Step 2 above (I.e., the original ASCII equivalent plus the random integer) exceeds N2, then subtract the largest possible multiple of N2 from this number, and add the remainder to N1.

    Hence the encoded number will always fall between N1 and N2, and will therefore always represent some ASCII character.

    4
    Display the characters that correspond to the encoded ASCII values.

    5.The procedure is reversed when decoding a line of text. Be certain, however, that the same random number is used in decoding as was used in encoding.

    Although it is simply a conversion with addition in no., I am still confused....

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Re: Re: I didn't understood you...

    Originally posted by moi
    problems:

    includes conio.h, a nonstandard header, for no good reason.
    uses randomize(), a nonstandard function.
    uses the "rand() % n" method to generate random numbers in a certain range which can be quite inaccurate

    The code can't be run in VC++, it showed a error message:

    error C2065: 'randomize' : undeclared identifier

    why?

    And can I use "time" to generate random numbers?
    What's the code?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Re: Re: I didn't understood you...

    Originally posted by stevenC
    The code can't be run in VC++, it showed a error message:

    error C2065: 'randomize' : undeclared identifier

    why?

    And can I use "time" to generate random numbers?
    What's the code?
    randomize is a NON standard function like moi said.
    You can use time to create pseudo random numbers. Just call srand with time( NULL ) as the parameter and then call rand() and mod it with your limit.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Re: Re: Re: Re: I didn't understood you...

    Originally posted by MrWizard
    randomize is a NON standard function like moi said.
    You can use time to create pseudo random numbers. Just call srand with time( NULL ) as the parameter and then call rand() and mod it with your limit.
    I still dont know how to embed it...
    Is it correct as follows?There is still error in VC++.
    Is there any C program run the following code without errors?



    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <conio.h>
    
    
    unsigned int a[80],ran,n=0,t,len=0,asc[80],n1=0,n2=127;
    char input[80],output[80],ch;
    
    
    
    
    int main(void){
    	
    /*->*/	srand( (unsigned)time( NULL ) );
    	
    	printf("This is a text encoding program.\n");
    	printf("Please enter text wanted to be encoded.\n");
    
     /*->*/   ran= srand() ;	//a random no. is generated
    
    	printf("%d\n",ran);
    	gets(input); //receive data input
    	
    	len=strlen(input);	//calculate the length of text inputted
    
    	printf("There are %d characters inputted(include blank space).\n",len);	
    
    	//
    
    	
    	for(int i=0;i<len;i++){ 
    		asc[i]=input[i];	//convert char to int 1st
    		asc[i]+=ran;		//encoding by addition with a random no.
    		
    		while(asc[i]>n2){//the loop ensure the no. is inbetween ASCII range
    			if(asc[i]>=0){
    				asc[i]-=n2;	  
    				a[i]++;
    			}
    		}
    	
    	}
    
    	
    	printf("The text is encoded as follows:(include blank space) \n");
    
    	for(int j=0;j<len;j++) printf("%d ",asc[j]);
    	//printf("\n%d",ran);
    	printf("\n\n");
    
    	printf("Do you want to decode the statement above?(y/n): "); //ask user of decoding
    	scanf("%c",&ch);  //receive command
    	if((ch=='y')||(ch=='Y')){	//if confirm, start decoding
    		for(int u=0;u<len;u++){		//return to original text for each character as follows:
    			if (a[u]!=0){
    				output[u]=a[u]*n2+asc[u]-ran;	//(for char exceed ASCII before encode)
    				
    			}
    			else
    				output[u]=asc[u]-ran;		//(for char inbetween ASCII before encode)
    		}
    		printf("The original text is:\n");
    		for (int k=0;k<len;k++) printf("%c",output[k]);	//print out the original text
    		printf("\n");
    	}
    
    return 0;
    }
    
    
    error C2065: 'time' : undeclared identifier
    error C2660: 'srand' : function does not take 0 parameters
    Code tags added by kermi3

  9. #9
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    Thank you kermi3

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>ran= srand() ;
    Incorrect use of srand, and I expect you meant rand() anyway. Look:
    >ran = rand() % MAX_NUMBER;
    But make sure you read this about poor quality random sequences.

    >>for(int i=0;i<len;i++){
    This is c++ style coding, and is not acceptable for C89 standard C. You cannot declare a variable within the for statement. So, its:
    >>int i;
    >>for(i=0;i<len;i++){

    >>if(asc[i]>=0){
    asc is an array of unsigned int's, therefore, by definition, the value will always be >= 0.


    You need to include time.h for the time() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Encoding charset to use card suits ASCII characters
    By Nazgulled in forum C Programming
    Replies: 23
    Last Post: 06-10-2007, 06:19 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. help..random pattern Ascii
    By equlizer in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 03:01 AM