Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-10-2003, 02:02 PM   #1
Registered: Sep 2003
Location: Chicago
Distribution: Slackware 14.1
Posts: 60

Rep: Reputation: 15
C, converting hex to text

ok, i appologize for the subject, i'm a newbie coder, and don't really know my way thru alot of this. Anyway, could anyone help me with this little scenario:

i have a form on a webpage, the webpage is driven by a C compiled program that i have been working on, the form passes the arguments on a "query string".. but all non alpha-numeric charactors are converted to %hexvalue.

i was wondering if there was a simpler way to strip off the % sign, take the 2 hex digits that follow the %, and print them back out as the characters they are supposed to represend.. per say,
blah blah blah d'oh
fix me

^--- something along the lines of that.

i've tried a few methods, all with undesireable results or segfaults.

Last edited by Sm0k3; 10-10-2003 at 02:04 PM.
Old 10-10-2003, 08:53 PM   #2
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
At a guess,

You could use atoi to convert the %27 to an int.

const char *theValueStr="%27";

char theChar = (char) atoi(theValueStr+1); // the +1 will move the pointer passed the '%';

std::cout << theChar;

Also note that your %00 will give you a null and not a \n (newline)... as you example implies that %00 will give you a \n.. I think a \n is 10. Do a 'man ascii' to get a list of ascii codes.

Old 10-11-2003, 04:29 AM   #3
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
you can first find the % sign and then use strtol():

long strtoul ( const char * string, char** endptr, int radix );
just replace a 0x instead of % and then call
myInteger = strtoul (YourStr, NULL, 10);
Now you can:
char Ch = myInteger;
You have now your original character in Ch.

Here is borrowed from anbout strtoul


unsigned long strtoul ( const char * string, char** endptr, int radix );
Convert string to unsigned long integer.
Parses string interpreting its content as an integer value until a character that can not be interpreted is found, and returns an unsigned long value.


String representing an integer number. The number is considered until a non-numeric character is found (only the digits from 0 to radix-1 are considered valid numeric characters, signs are considered valid numeric characters for this parameter). The format used is:
(where whitespaces are any tab or space character and nnnnn is a sequence of valid numbers following the specified radix).
Although the value returned is unsigned, signs in the string will be read and considered.
Address of a pointer. This is filled by the function with the address where scan has ended. Serves to determine where there is the first non-numerical character in the string.
Numeral radix in which the number to be interpreted. Must be 0 or be between 2 and 36. If it is 0 the radix of the string is determined by the initial characters of the string:
initial chars Interpreted by strol as
0x hexadecimal: radix 16
0 octal: radix 8
any number from 1 to 9 decimal: radix 10

Return Value.
The converted unsigned long value from the input string.
If conversion would cause overflow the result is ULONG_MAX.
If an error occurs or no conversion can be made 0 is returned.

Defined in ANSI-C.


/* strtoul example */
#include <stdio.h>
#include <stdlib.h>

int main ()
char szInput [256];
char * pEnd;
unsigned long ul;
printf ("Enter an integer value: ");
gets (szInput);
ul = strtoul (szInput,&pEnd,0);
printf ("Value entered: %lu. Its double: %lu\n",ul,ul*2);
return 0;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting Windows text to Linux text joshknape Linux - Software 3 09-11-2005 12:52 PM
gnome-calculator - converting between numeric bases (eg hex to binary) jht2k Linux - General 2 07-01-2005 09:12 AM
view text file in hex under kdevelop tcma Linux - Software 1 10-20-2004 05:57 PM
converting fake hex to decimal in c acid_kewpie Programming 10 08-20-2003 02:29 PM
Converting Text To HTML Glock Shooter Programming 6 07-03-2002 06:08 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration