Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 20th, 2008, 2:25 PM   #11
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How Do I Urlencode A String In C?

Here we go. The only other thing that I can see of concern is if dest is attempted to be written to past the end of the array. But since I don't personally need to watch out for that, I'm leaving out a length restriction. I'm just warning anyone who stumbles across this thread and finds the code, to be careful that dest isn't written to past the end of the array.

C Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int c_isalnum(const char c) {
  5. return c>='0'&&c<='9' || c>='a'&&c<='z' || c>='A'&&c<='Z';
  6. }
  7.  
  8. int urlencode(char *dest, const char *src) {
  9. /* urlencode all non-alphanumeric characters in the C-string 'src'
  10.   store result in the C-string 'dest'
  11.   return the length of the url encoded C-string
  12.   */
  13. char *d;
  14. int i;
  15. for(i=0, d=dest; src[i]; i++) {
  16. if(c_isalnum(src[i])) *(d++) = src[i];
  17. else {
  18. sprintf(d, "%%%02X", src[i]);
  19. d += 3;
  20. }
  21. }
  22. *d = 0;
  23. return d-dest;
  24. }
  25.  
  26. int main() {
  27. char buff[1024];
  28. int len;
  29.  
  30. len = urlencode(buff, "Hello 2 The World! $%^&*()");
  31. printf("[%d] %s\n", len, buff);
  32.  
  33. return 0;
  34. }
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 20th, 2008, 2:29 PM   #12
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: How Do I Urlencode A String In C?

When I compiled and ran your program this is what I got (VC++ 2008 Express on Vista)

Quote:
[50] Hello%202%20The%20World%21%20%24%25%5E%26%2A%28%29
Press any key to continue . . .
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Mar 20th, 2008, 2:34 PM   #13
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How Do I Urlencode A String In C?

That looks right. Thanks.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.

Last edited by Sane; Mar 20th, 2008 at 2:44 PM.
Sane is offline   Reply With Quote
Old Mar 20th, 2008, 5:46 PM   #14
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Re: How Do I Urlencode A String In C?

Is there a particular reason you didn't use isalnum() from ctype.h and wrote your own?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 20th, 2008, 8:42 PM   #15
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How Do I Urlencode A String In C?

It's C99. But I need things to be as compatible as possible. I'll also be asking you people to help me translate some snippets (this included) to Java and other languages. You guys will see why in 1 or 2 days. I think Infinate knows why.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 20th, 2008, 8:49 PM   #16
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Re: How Do I Urlencode A String In C?

Quote:
Originally Posted by Sane View Post
It's C99. But I need things to be as compatible as possible.
It isn't, AFAIK. But even if it was (and I'm wrong), you could just do
c Syntax (Toggle Plain Text)
  1. isalpha(ch) || isdigit(ch)

According to my man-pages, all of the functions in ctype.h are actually macros that do table lookups. They're fairly efficient (probably more so than your function), and I don't see any reason to not use them.

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 20th, 2008, 9:06 PM   #17
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: How Do I Urlencode A String In C?

Quote:
Originally Posted by Sane View Post
It's C99. :
No -- its been around for many many years, as far back as I remember.
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Mar 20th, 2008, 11:52 PM   #18
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How Do I Urlencode A String In C?

Oh, I just checked. It isn't C99. I was just thrown off by Ooble who said "It is". Sorry.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 21st, 2008, 2:02 AM   #19
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: How Do I Urlencode A String In C?

I thought you meant "is it part of C99?" not "was it introduced by C99?" Apologies for the confusion.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 2:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 3:04 PM
Help with breaking apart a string csrocker101 C# 6 Apr 6th, 2007 8:50 AM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 7:55 PM
Problems with String to MD5 Conversion emdiesse Visual Basic .NET 0 Feb 2nd, 2006 11:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC