Thread: need help understanding this function

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    need help understanding this function

    Hi all I'm trying to figure out how this function works, pointers give me a hard time still especially a function with several pointers as parameters.

    I understand the function takes a string and if it has a % or + it removes those, I don't understand the nibble part of the function is where I'm confused. Long story short guess I just wonder if anyone could comment it so I can understand what's happening.

    Code:
    /* Unencode a urlencoded string*/
    /*----------------------------------------------------------------------------*/
    static void unencode(char *dst,char *src,size_t len)
    {	char c;
    	while (c = *src++,len--)
    	{	if (c == '+') c = ' ';
    		else if (c == '%')
    		{	c = *src++;
    			c = (nibble(c) << 4) | nibble(*src++);
    			len -= 2;
    		}
    		*dst++ = c;
    	}
    	*dst = '\0';
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well a nibble is just 4 bits of a byte, and can be represented as a single character 0-9A-F.

    The &#37; in URL encoding is followed by 2 hex digits, say %41 would be a long winded way of saying 'A'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Doing a bit of research on urlencodeing and decoding you have a string with a single char being represented by a &#37; and a 2 digit hex code for the actual char in string format so "%22" would be equal to " and "%28" would equal to (

    Since the hex digits are in string format you have to convert the ascii char '2' into the actual hex number. A nibble is just a single hex digit. A high and low nibble represent a single char.

    So it is just a string to hex conversion.


    [edit]
    That isn't very portable is it? I mean wouldn't there be endian issues?
    Last edited by manofsteel972; 08-04-2007 at 12:40 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manofsteel972 View Post
    Doing a bit of research on urlencodeing and decoding you have a string with a single char being represented by a % and a 2 digit hex code for the actual char in string format so "%22" would be equal to " and "%28" would equal to (

    Since the hex digits are in string format you have to convert the ascii char '2' into the actual hex number. A nibble is just a single hex digit. A high and low nibble represent a single char.

    So it is just a string to hex conversion.


    [edit]
    That isn't very portable is it? I mean wouldn't there be endian issues?
    Endian issues only arise if you have "words" longer than a byte. Since all (that I know of) encodings used for URL's are single-byte, it shouldn't be a problem - I know of no processor that stores NIBBLES "the wrong way around".

    --
    Mats

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Ok. So a single byte char is the only data type not affected by endian issues. And wide char or multibyte unicode isn't affected either since it is still handled as bytes right?

    [edit] Answered my own question. A wide char is affected by endian issues.
    Last edited by manofsteel972; 08-04-2007 at 10:46 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM