short but complicated ascii code

parameter passed into unescape_url() is
'http://hlcbsas/cgi-bin/hi_iq_func_test?config=%2Fsrv%2Fibis-q%2Fmodules%2Fmort%2FMort_CV.cfg&sas=0&test=0&func=AgeRate_cv.def
&YearProxy=Year9&Year9=2008-2009&Year9=2004-2007&CauseDeathProxy=ICDNCHS50&GeoProxy=GeoLHD&cross1=YearProxy'

void unescape_url(char *url) {
register int x,y;

for(x=0,y=0;url[y];++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}


char x2c(char *what) {
register char digit;

digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}

i found this link that explains the digit = code. but what does it mean when you multiply the upper case of the char by 16?
http://answers.yahoo.com/question/index?qid=20100427161810AASL7R2
lets disregard x2c(), for now. we know it returns a char that will go in url[x]

first instance of '%' is at lets say x=33, y=33. for statement is true at this point.
url[33] is modified to any char for now ='9'
x=33, y=35
x=38,y=40 when the next '%' is encountered and so on.
they both keep incrementing by 1 and the for condition is never met.

am i missing something, cause this does not seem to have a point.
This is what we developers call clever code. Clever code is typically:
1. fast
2. concise
3. correct when written
4. almost impenetrable, and thus difficult to amend

You'll need to understand what the code is doing.

The code is applying the transformation described here:
http://www.w3schools.com/tags/ref_urlencode.asp

You need to undertand the ASCII table and the relationship between the characters:
http://www.asciitable.com/index/asciifull.gif

The expression:
(what[0] - '0'))
returns the integer value of a character in the range 0-9.

The expression:
((what[0] & 0xdf) - 'A')+10
returns the integer value of a hex character in the range A-F or a-f. The 0xDF mask is used to convert 'a' to 'A'. Like the answers post said, toupper could be used, but this is more consice once you know what it's doing.

So:
(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0')) converts each hex character to a hex digit.
Mulplying by 16 shifts the digit over because we're dealing with base 16.

x2c converts a hex 2 character string to decimal
unescape_url edits, in place, the URL string passed in as replace escape sequences with their correct characters.
Last edited on
that is very helpful, thankyou. i appreciate your taking the time to look into this.
i resolved the first occurence of '%' as follows
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
what[0]='2' digit resolves to 2
32=digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
what[1]='F' resolving to 70, 'A'=65; 70-65+10=15
15+32=47
i think i got the x2c() part, but if i did not i will look again.

as i mention in my 2nd comment, lets disregard the x2c part as it only complicates the question on the logic of the initial function of unescape_url. by my logic which must be flawed it only access's the x2c function once
Topic archived. No new replies allowed.