frustration on URL encode-decode

This is a discussion on frustration on URL encode-decode within the C Programming forums, part of the General Programming Boards category; The function URLEncode spits out %20 for a space which, while is correct, should be '+' instead. I've spent a ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The function URLEncode spits out %20 for a space which, while is correct, should be '+' instead. I've spent a couple hours trying to figure out how to do it, but to no avail.
    Check if source[i] is a space. If so, encode it to '+' instead of encoding it to %20. I might just add another function, say isEncodable(), that handles the checking for whether the character is to be encoded. Then, your encoding loop could become something like:
    Code:
    for (i = 0; (destc + 1 < length) && source[i]; i++) {
        if (source[i] == ' ') {
            destination[destc] = '+';
            destc++;
        } else if (isEncodable(source[i])) {
            if (destc + 3 >= length) {
                break;
            }
    
            destination[destc] = '%';
            destination[destc + 1] = hexa[source[i] / 16];
            destination[destc + 2] = hexa[source[i] % 16];
            destc += 3;
        } else {
            destination[destc] = source[i];
            destc++;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I gave it shot utilizing that method of trying to get the program to change a space into a +, but nothing is working.

    I've tried to put this bit of code in several placed throughout the function thinking it would be fine, but no luck.. grrr it doesn't make sense, I've commented where I think it should logically go.

    Code:
    if(source[i] == ' ') {
       destination[destc] = '+';
    }
    Code:
    void URLEncode(char* destination,int length, char* source){
      //local variables
       int destc=0;
       int i;
       int a;
       int clear=0;
    
        for(i=0;destc+1<length&&source[i];i++){
            clear=0;
        for(a=0;nonencode[a];a++){
           if(nonencode[a]==source[i]){
              clear=1;
              break;
            }
            //snippet to change space to + should work fine here but doesn't
        }
         if(!clear){
            if(destc+3>=length){
               break;
            }
            destination[destc]='&#37;';
            destination[destc+1]=hexa[source[i]/16];
            destination[destc+2]=hexa[source[i]%16];
            destc+=3;
        }
          else{
              destination[destc]=source[i];
              destc++;
           }
        }
        destination[destc]='\0';
    }
    Then I tried lasterlight's suggestion to make another function called isEncodable to take some code of out the URLEncode function but I'd rather keep it the way it is without calling another function, then I had to leave for class and haven't had a chance to really get back to it.

    Code:
    // not real function just an idea
    void isEncodeable(char* source) {
      //local variables
       int destc=0;
       int i;
       int a;
       int clear=0;
    
        for(i=0;destc+1<length&&source[i];i++){
            clear=0;
        for(a=0;nonencode[a];a++){
           if(nonencode[a]==source[i]){
              clear=1;
              break;
            }
        }
    }
    Thanks again for the help with this nagging problem

    had another idea of trying this

    Code:
    if(destination[destc] == '%20') {
      destination[destc] = '+';
    }
    and putting that right after the encoding part of the function but that didn't work either
    Last edited by Lince; 08-21-2007 at 08:41 PM. Reason: had another idea

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I gave it shot utilizing that method of trying to get the program to change a space into a +, but nothing is working.
    I tried my own code snippet and it works fine when integrated into your code. The catch is that your destination string may not be long enough to hold the encoded string, but that is acceptable for now since you are just figuring out a basic algorithm.

    Then I tried lasterlight's suggestion to make another function called isEncodable to take some code of out the URLEncode function but I'd rather keep it the way it is without calling another function, then I had to leave for class and haven't had a chance to really get back to it.
    Notice that my code snippet passes source[i] to isEncodable(). source[i] is a char, and indeed isEncodable() checks if the char is encodable. The implementation of isEncodable itself is very simple: loop over nonencode. If the char passed matches a char in nonencode, return 0. If there are no matches, return 1.

    had another idea of trying this
    Single quotes are used for delimiting a char, so '&#37;20' does not make sense. "%20" will not work either, since then you would be comparing a char to a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    I'm attempting to implement Laserlight's idea, but I can't get it to work. I really don't understand what I'm doing wrong here, I suppose I'm getting to frustrated to look at it line by line.

    Code:
    /*
    void URLEncode(char* destination,int length, char* source){
    	//local variables
        int destc=0;
    	int i;
    	int a;
    	int clear=0;
    
        for(i=0;destc+1<length&&source[i];i++){
            clear=0;
            for(a=0;nonencode[a];a++){
                if(nonencode[a]==source[i]){
                    clear=1;
                    break;
                }
            }
            if(!clear){
                if(destc+3>=length){
                    break;
                }
    
                destination[destc]='%';
                destination[destc+1]=hexa[source[i]/16];
                destination[destc+2]=hexa[source[i]%16];
                destc+=3;
            }
    			
            else{
                destination[destc]=source[i];
                destc++;
            }
        }
        destination[destc]='\0';
    }
    */
    int isEncodable(char* sourceChar) {
    	//local variables
    	int i= 0;
    	int a = 0;
    	int clear = 0;
    
    	for(a=0;nonencode[a];a++) {
    		if(nonencode[a]== sourceChar[i]) {
    			clear = 0;
    		}
    		else {
    			clear = 1;
    		}
    	}
    	return clear;
    }
    
    void URLEncode2(char* destination,int length, char* source){
    	//local variables
        int destc=0;
    	int i = 0;
    		
    	for (i = 0; (destc + 1 < length) && source[i]; i++) {
    		if (source[i] == ' ') {
    			destination[destc] = '+';
    			destc++;
    		} else if (isEncodable(source[i])) {
    			if (destc + 3 >= length) {
                break;
            }
    
            destination[destc] = '%';
            destination[destc + 1] = hexa[source[i] / 16];
            destination[destc + 2] = hexa[source[i] % 16];
            destc += 3;
    	} else {
            destination[destc] = source[i];
            destc++;
        }
    }
    
    }
    errors
    E:\URLcoder.c(224) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
    E:\URLcoder.c(224) : warning C4024: 'isEncodable' : different types for formal and actual parameter 1

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    figured this part out

    I got mad and I just couldn't leave it alone, I got it to work using this instead.

    Code:
    void URLEncode(char* destination,int length, char* source){
    	//local variables
        int destc=0;
    	int i;
    	int a;
    	int clear=0;
    
        for(i=0;destc+1<length && source[i];i++){
    		if (source[i] == ' ') {
    			destination[destc] = '+';
    			destc++;
    			}
    		else {
            clear=0;
            for(a=0;nonencode[a];a++){
                if(nonencode[a]==source[i]){
                    clear=1;
                    break;
                }
            }
            if(!clear){
                if(destc+3>=length){
                    break;
                }
    
                destination[destc]='%';
                destination[destc+1]=hexa[source[i]/16];
                destination[destc+2]=hexa[source[i]%16];
                destc+=3;
            }
    			
            else{
                destination[destc]=source[i];
                destc++;
            }
        }
        destination[destc]='\0';
    	}
    }

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    errors
    E:\URLcoder.c(224) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
    E:\URLcoder.c(224) : warning C4024: 'isEncodable' : different types for formal and actual parameter 1
    You need to learn to interpret error messages
    The error messages are trying to tell you that the function's parameter is a char*, but you are passing a char. The solution is quite simple: you actually want the parameter to be a char, since passing a char as the argument is correct.

    Next, your implementation of isEncodable() is not correct. Notice that the algorithm I outlined states that you should return 0 as soon as the char passed matches a char in nonencode. This means that you should either break out of the loop when that happens, or return from the function immediately. As such, a correct implementation of isEncodable() would be:
    Code:
    int isEncodable(char sourceChar) {
        int i;
        for (i = 0; i < sizeof nonencode; i++) {
            if (nonencode[i] == sourceChar) {
                return 0;
            }
        }
        return 1;
    }
    Now, the implementation of URLEncode() is simple:
    Code:
    void URLEncode(char *destination, int length, char *source) {
        int destc = 0;
        int i;
        for (i = 0; (destc + 1 < length) && source[i]; i++) {
            if (source[i] == ' ') {
                destination[destc] = '+';
                destc++;
            } else if (isEncodable(source[i])) {
                if (destc + 3 >= length) {
                    break;
                }
    
                destination[destc] = '&#37;';
                destination[destc + 1] = hexa[source[i] / 16];
                destination[destc + 2] = hexa[source[i] % 16];
                destc += 3;
            } else {
                destination[destc] = source[i];
                destc++;
            }
        }
        destination[destc]='\0';
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    Red face

    Thank you Laserlight, I didn't understand but that makes sense, going through each element of the array and comparing, instead of just trying to paste the code into another function, when I get home I'll implement it that way.

  8. #23
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    307
    What is Joe Blow, 1234 Some Drive, Anywhere, OH's URL?
    GCC on Fedora Core 10

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encode and Decode algorith for 2 to 7 bits
    By kingofpop in forum C Programming
    Replies: 2
    Last Post: 11-15-2008, 06:08 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Url query encoding/decoding
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-25-2007, 03:30 PM
  4. File I/O Encode & Decode with XOR
    By DarrenY in forum C# Programming
    Replies: 2
    Last Post: 04-12-2007, 04:31 AM
  5. suggestions : encode / decode header + documentation
    By gamer in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 04:17 AM

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