LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 10-10-2003, 02:02 PM   #1
Sm0k3
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Slackware-current
Posts: 63

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,
Code:
mywebsite.org/cgi-bin/mycgi?text=blah+blah+d%27oh%00fix+me
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
mr_segfault
Member
 
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.

Cheers..
 
Old 10-11-2003, 04:29 AM   #3
Mohsen
Member
 
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
Code:
myInteger = strtoul (YourStr, NULL, 10);
Now you can:
Code:
char Ch = myInteger;
You have now your original character in Ch.


Here is borrowed from www.cplusplus.com anbout strtoul

Quote:

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.

Parameters.

string
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:
[whitespaces][+|-][0|0x][nnnnn]
(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.
endptr
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.
radix
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.

Portability.
Defined in ANSI-C.


Example.


/* 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;
}
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 AM.

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