ASCII encoding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • palm
    New Member
    • Feb 2007
    • 27

    #1

    ASCII encoding

    Hi There,
    I need to encode some strings in C program before I pass them to url. Can some one help me please?

    Palm.
  • palm
    New Member
    • Feb 2007
    • 27

    #2
    Hi there,
    I just wanted to share what I've found... This seems working fine:

    #include <stdio.h>
    #include <string.h>
    void main(){
    char a[]= "apple)ljdf d&";
    int y=0;
    for (int i=0; i< sizeof (a)-1; i++)
    {
    y= ("%x",a[i]);
    printf ("%%");
    printf ("%x",y);
    }
    }

    Thankz guyz.

    Palm.

    Comment

    • nmadct
      Recognized Expert New Member
      • Jan 2007
      • 83

      #3
      Good solution. Just a few of things I noticed.

      1. This will URL-encode every single character in the string. While this works, it's not necessarily what you want. You could skip alphanumeric characters (print them as-is rather than printing their hex codes) to make the output look cleaner.
      2. I think the hex codes are always supposed to be 2 digits long in URL encoding. Your code will backfire with ASCII codes less than 16, I think. Use the format specification "%02x" (that's a zero) to pad 1-character codes with a zero.
      3. I'm pretty sure instead of this: y= ("%x",a[i]);
      you meant this: y = a[i];

      Comment

      Working...