help to reverse this function

This is a discussion on help to reverse this function within the C++ Programming forums, part of the General Programming Boards category; Ok I have a function Code: int stylecodes(char *input, char *out, int max) { int c = 0; char *str ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    help to reverse this function

    Ok I have a function

    Code:
    int stylecodes(char *input, char *out, int max) {
    	int c = 0;
    	char *str = input;
    	memset(out,'\0',max);
    	unsigned int i = 0;
    	for (i = 0; i < strlen(input); ) {
    		if (strncmp(str,"%c%",3)==0) {
    			snprintf(out,max,"%s%c",out,3);
    			str+=3;
    			c++; i+=3;
    		} else if (strncmp(str,"%b%",3)==0) {
    			snprintf(out,max,"%s%c",out,2);
    			str+=3;
    			c++;i+=3;
    		} else if (strncmp(str,"%o%",3)==0) {
    			snprintf(out,max,"%s%c",out,15);
    			str+=3;
    			c++;i+=3;
    		} else if (strncmp(str,"%u%",3)==0) {
    			snprintf(out,max,"%s%c",out,31);
    			str+=3;
    			c++;i+=3;
    		} else if (strncmp(str,"%i%",3)==0) {
    			snprintf(out,max,"%s%c",out,22);
    			str+=3;
    			c++;i+=3;
    		} else {
    			snprintf(out,max,"%s%c",out,str[0]);
    			str++;
    			i++;
    		}
    	}
    	return c;
    }
    now I want a fuction, which makes these steps backwards. Lets say the fuction replaces the string %c% with the ascii code 3 then I need a function which replaces the ascii code 3 back to
    %c%

    THX for everyone who can help me!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    OK, so did you try to do it yourself already?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    yeah I try to change ,"%c%" to "" . That works but the character 31 for %u% I cant type in my programm.
    I need a command which converts a number to its ascii code. For example:

    Code:
    if (strncmp(str,chr(3),1)==0) {
    	snprintf(out,max,"%s%s",out,"%c%");
    	str+=1;
    	c++; i+=1;

    But c++ don't have a command like chr or I don't know it.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Now I try this

    Code:
    	const char *replace;
    	snprintf(replace,sizeof(replace),"%c",3);
    	for (i = 0; i < strlen(input); ) {
    		if (strncmp(str,replace,1)==0) {
    but

    error C2664: '_snprintf' : cannot convert parameter 1 from 'const char *' to 'char *'

    That's a big problem, because the function strncmp() want as second parameter a const char and I don't know how to give this function the ascii code 3. Please help me.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    if you wanna replace a char containing a number as an ascii code, just do int x = y - '0'; (assuming y's a char)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > snprintf(replace,sizeof(replace),"%c",3);
    Well replace doesn't point anywhere, and being a pointer, sizeof() doesn't do what you probably want it to do (assuming the pointer pointed somewhere)

    > and I don't know how to give this function the ascii code 3.
    Like
    Code:
    if ( strncmp ( buff, "%c\003", 3 ) == 0 )  // A character specified as 3 octal characters
    if ( strncmp ( buff, "%c\x03", 3 ) == 0 )  // A character specified as 2 hex characters

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    OK my code now looks so:

    Code:
    int stylecodesback(char *input, char *out, int max) {
    	int c = 0;
    	char *str = input;
    	memset(out,'\0',max);
    	unsigned int i = 0;
    	for (i = 0; i < strlen(input); ) {
    		if (strncmp(str,"%c\x03",1)==0) {
    			snprintf(out,max,"%s%s",out,"%c%");
    			str+=1;
    			c++; i+=1;
    		} else if (strncmp(str,"%c\x02",1)==0) {
    			snprintf(out,max,"%s%s",out,"%b%");
    			str+=1;
    			c++;i+=1;
    		} else if (strncmp(str,"%c\x0F",1)==0) {
    			snprintf(out,max,"%s%s",out,"%o%");
    			str+=1;
    			c++;i+=1;
    		} else if (strncmp(str,"%c\x1F",1)==0) {
    			snprintf(out,max,"%s%s",out,"%u%");
    			str+=1;
    			c++;i+=1;
    		} else if (strncmp(str,"%c\x16",1)==0) {
    			snprintf(out,max,"%s%s",out,"%i%");
    			str+=1;
    			c++;i+=1;
    		} else {
    			snprintf(out,max,"%s%c",out,str[0]);
    			str++;
    			i++;
    		}
    	}
    	return c;
    }
    The 3 ascii character get replaced by %c, but it should look like this %c% and all other characters like 2 ascii to %b% are don't get replaced, whats wrong?
    Last edited by keeper; 02-08-2006 at 09:33 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > if (strncmp(str,"%c\x03",1
    Well there are 3 chars in your string, not 1.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Salem
    > if (strncmp(str,"%c\x03",1
    Well there are 3 chars in your string, not 1.

    Hmm I understand it that way the %c\x03 represented the third ascii code. That is what I'm searching for in my string and this have a len of 1. When I write if (strncmp(str,"%c\x03",3
    noting happens the char is as it was before.

    I need something which finds the third ascii code. I looked that up in the msdn

    http://msdn.microsoft.com/library/en...ml/vc38qo1.gif

    and what I need is the ETX Code. I want to search for that in my text an replaces it with %c%

    This is an example:

    [7 %test% 3]

    And the things before the "7" and the "3" I want to replace with %c%

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    "%c\x03"
    %
    c
    \x03

    Are 3 characters.

    Perhaps you're matching entirely the wrong thing.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Salem
    "%c\x03"
    %
    c
    \x03

    Are 3 characters.

    Perhaps you're matching entirely the wrong thing.
    Ok I see it %c\x03 cant be ascii code 3, because this function replaces every % in my text with a %c%. That is not really what I want. I only want to replace:

    ascii3 with %c%
    ascii2 with %b%
    ascii15 with %o%
    ascii31 with %u%
    ascii22 with %i%

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I might choose this type of approach.
    Code:
    #include <string.h>
    
    static const struct style
    {
        const char *key;
        int value;
    } Style_Code[] =
    {
        {"%c%",  3},
        {"%b%",  2},
        {"%o%", 15},
        {"%u%", 31},
        {"%i%", 22},
    };
    
    int foo(const char *in, char *out, int max)
    {
        char *start = out;
        while ( *in != '\0' && max > 0 )
        {
            size_t i;
            for ( i = 0; i < sizeof Style_Code / sizeof *Style_Code; ++i )
            {
                size_t keylen = strlen(Style_Code[i].key);
                if ( strncmp(in, Style_Code[i].key, keylen) == 0 )
                {
                    *out++ = Style_Code[i].value;
                    in += keylen;
                    break;
                }
            }
            if ( i >= sizeof Style_Code / sizeof *Style_Code )
            {
                *out++ = *in++;
            }
            --max;
        }
        *out = '\0';
        return out - start;
    }
    
    int bar(const char *in, char *out, int max)
    {
        char *start = out;
        while ( *in != '\0' && max > 0 )
        {
            size_t i;
            for ( i = 0; i < sizeof Style_Code / sizeof *Style_Code; ++i )
            {
                if ( *in == Style_Code[i].value)
                {
                    size_t keylen = strlen(Style_Code[i].key);
                    strcpy(out, Style_Code[i].key);
                    out += keylen;
                    max -= keylen - 1;
                    ++in;
                    break;
                }
            }
            if ( i >= sizeof Style_Code / sizeof *Style_Code )
            {
                *out++ = *in++;
            }
            --max;
        }
        *out = '\0';
        return out - start;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Perfect Dave_Sinkula works exactly the way I want this function work. Thx alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21