Results 1 to 8 of 8

Thread: any ideas (integer encryption)

  1. #1

    Thread Starter
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    any ideas (integer encryption)

    I'm trying to come up with a way to take a buffer of integers such as the numbers '123456' and translate them singly into their character reprenstation form....


    for use with network hand shake of a server/client I am creating.

    I've tried quite a few ways of looping through each index of a character array to do this, but to no avail....


    Code:
    int CreateHandShake(char *buf)
    {
    	char data[10];
    	char a22[1];
    	char *b22;
    	strcpy(data, "123456");
    	b22 = data;
    	for(int cnt=0; cnt < 4; cnt++)
    	                {
    		strncpy(a22,b22,1);
    		b22 + 1;
    		strcpy(buf,a22);
    		strcpy(a22, "");
    		}
       return 0;
    }

    any ideas? anyone?
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: any ideas (integer encryption)

    Originally posted by TokersBall_CDXX
    I'm trying to come up with a way to take a buffer of integers such as the numbers '123456' and translate them singly into their character reprenstation form....


    for use with network hand shake of a server/client I am creating.

    I've tried quite a few ways of looping through each index of a character array to do this, but to no avail....


    Code:
    int CreateHandShake(char *buf)
    {
    	char data[10];
    	char a22[1];
    	char *b22;
    	strcpy(data, "123456");
    	b22 = data;
    	for(int cnt=0; cnt < 4; cnt++)
    	                {
    		strncpy(a22,b22,1);
    		b22 + 1;
    		strcpy(buf,a22);
    		strcpy(a22, "");
    		}
       return 0;
    }

    any ideas? anyone?
    not sure what you're trying to do here, but
    b22 + 1;
    does nothing, you probably mean ++b22;
    strncpy(a22,b22,1);
    is the same as
    *a22=*b22;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    I'm trying

    to run through each number in the buffer, and translate it into it's character representation.

    I was using strncpy() to return 1 character from the buffer..

    hmm still stumped..
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  4. #4

    Thread Starter
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    I've tried type casting as well,

    Code:
    int CreateHandShake(char *buf, HWND hDlg2)
    {
    	char data[10];
    	char a22[1];
    	int c22;
    	char *b22;
    	strcpy(data, "123156");
    	b22 = data;
    	for(int cnt=0; cnt < 6; cnt++)
    		{
    		c22=(int)data[cnt];
    		buf[cnt] = (char)c22;
    		}
       return 0;
    }
    doesn't translate the character though
    buf = 123156
    instead of the character representation?

    hmmm...
    any ideas?
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    Re: I've tried type casting as well,

    Use atoi() defined in the <cstdlib> header to convert from text to int.

  6. #6

    Thread Starter
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: Re: I've tried type casting as well,

    Originally posted by transcendental
    Use atoi() defined in the <cstdlib> header to convert from text to int.

    Code:
    {
    	char data[10];
    	char a22[1];
    	int c22;
    	strcpy(data, "123156");
    	for(int cnt=0; cnt < 6; cnt++)
    		{
    		c22=atoi(data[cnt]);
    		buf[cnt] = (char)c22;
    		}
       return 0;
    }

    gives me an error because data[cnt] isn't a buffer/string ending with /0....

    Error example.c: 423 type error in argument 1 to `atoi'; found `char' expected `pointer to char'
    Compilation + link time:3.5 sec, Return code: 1

    I've already tried that..hmmm

    I've also tried using a pointer, and stepping the location of the memmory address incremented to each character of data.... also doesn't work.
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by TokersBall_CDXX
    doesn't translate the character though
    buf = 123156
    instead of the character representation?

    hmmm...
    any ideas?


    123156 is already the char representation since buf is a char array.

    I think you are utterly confused with numbers and their ascii character representations. Whatever numbers you sees on your computer, let it be your web browser, VC IDE or anything, are already in their textual form. It is just that they don't display them inside double quotes. We need to put double quotes when programming is because we want the compiler to know that literal is a string. And when compiling, compilers automatically convert for integer or float , from the textual form we type in our source to their equivalent binary form.

    An simple example to explain
    Code:
    int i=20;
    cout<< i <<endl;// display 20
    The 20 displayed on the console is text converted from int by cout.

    Another explanation

    123156 is text. It takes up 6 bytes.

    1234563 is text. It takes up 7 bytes.

    Integer type only takes up 4 bytes(32 bit).

  8. #8
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    If you have the character '1' and you want the integer value 1, you can simply do it like so (assuming all your numbers are 0-9):
    Code:
    char c = '1';
    int i = c - '0';   // equivalent to  '\1'
    cout << "i is " << i << endl;
    of course, if you're just displaying it there's no need for this, but i gathered from your question that you want to perform mathematical operations on it.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width