![]() |
How Do I Urlencode A String In C?
I know the function in other languages is "urlencode". Is there a library, or function I can call in C to urlencode a string? For example, turning spaces into %20?
|
Re: How Do I Urlencode A String In C?
I don't think there is an equivalent in the C standard library. There are a few implementations about online. But you're prob best writing your own, its not too difficult.
|
Re: How Do I Urlencode A String In C?
Could one essentially make a very naive implementation, by replacing every single character with its two-digit hex value? For example, with the string 's', printf("%02X", s[i]) for every 'i' from 1 .. strlen(s)? What are the possible drawbacks from this (besides wasted space)? And are there ways to slightly improve this method?
|
Re: How Do I Urlencode A String In C?
Quote:
|
Re: How Do I Urlencode A String In C?
What character set/encoding are you using?
You could create a table of strings, and each character value mapped to a string. Then you run each character of the URL through a function that uses it as a lookup into the table, and replaces it with the associated string. You could save space by using NULL for ones that didn't need conversion (alphanumeric characters, forward slashes, etc). The only problem I see with this approach is the table gets really big when you're using a character set besides plain ASCII. |
Re: How Do I Urlencode A String In C?
Just plain ASCII. And the urlencode is only being processed on the GET arguments of the URL, before plugging them into the URL to be sent (in which case, characters like '&' do need to be encoded).
|
Re: How Do I Urlencode A String In C?
I think you're right:
printf (or, rather, sprintf) is the way to go. For complete safety, I'd recommend doing essentially as Jimbo says and only parsing it if isalnum returns false - that way, you won't have to convert the majority of characters but you'll still catch anything that could even potentially cause a problem. |
Re: How Do I Urlencode A String In C?
Thanks guys.
:
|
Re: How Do I Urlencode A String In C?
Forgot to add:
:
Which may or may not be C99. I forget. Will check the reference when I get home if someone doesn't correct me before then. So I may have to swap isalnum with some other functions. |
Re: How Do I Urlencode A String In C?
It is. A point of interest: I was just reading the Wikipedia entry on it to find out the answer to your question, and found this:
Quote:
|
| All times are GMT -5. The time now is 4:10 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC