# Thread: need help understanding this function

1. ## 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. 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'

3. 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.

That isn't very portable is it? I mean wouldn't there be endian issues?

4. Originally Posted by manofsteel972
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.

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. 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?

 Answered my own question. A wide char is affected by endian issues.