Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to hex2ascii???

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HI
im a newbie
i want a source code that converts the hex into ascii, without using the console prompt...
for example: i have 2 variables:
char mychar1="96";
char mychar2;

how can i put mychar1 hex value in mychar2 (naturally in ascii character)

at the end i'll have mychar1="96" and mychar2="i"

Can anyone help me please???

Thanx a lot!
 
are you asking 'how do i convert hex to decimal ?'. If so, the answear is to write a simple function. I don't know of any library containing one. If you just want to print, all you do is 'printf("hex is %x decimal is %d\n", mychar, mychar);
 
yes, you understood what i mean... but i have to do that, printing nothing in the console...i have to print the result in a variable, not in the console... how to?
 
If you want to print your results into a variable,you can use the function "sprintf".

char mychar1="96";
char mychar2;
char Buffer[100]; /*this "buffer" will receive the results*/

....
.....
sprintf(Buffer,"hex is %x decimal is %d\n", mychar, mychar);/*prints the results into the "buffer"*/
 
your code works only if i set mychar="0x69"(69, and not 96, becuse i wronged the number when i posted the question the first time)
...the number 69 is an hex value...0x69 is equal to "i"...
if i write:

char mychar1="69";
char Buffer[100];

sprintf(Buffer,"hex is %x decimal is %c\n", mychar1, mychar1);

at the and i have Buffer="hex is (the corresponding hex value of "69") decimal is (the corresponding decimal value of "69")! instead i wanna have Buffer="hex is 69 decimal is i"...because i want to keep the number 69 as an hex value...
you understand what i mean?
 
Maybe this will do !?

void main( void )
{
int value1 = 0x69;
char Buffer[100];
sprintf(Buffer,"C hex: 0x%x\n decimal is %d\n", value1, value1);
printf("%s",Buffer);/*just for verification*/
}
 
If your trying to take a character string "69" (in hex) and make a decimal representation (105 in decimal).

This is just something to think about. I have not fully tested it ... just whipped something up. I'm sure someone can do better ;-)


Try this(take a string and figure out the dec value):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main()
{
	int	i = 0;

	long	int dec = 0;

	double	hold = 0.0;

        char    hex[] = &quot;69\0&quot;,
		b[64];

	strcpy(b,hex);

	for(i = (strlen(hex) - 1); i >= 0; i--)
	{
		switch(hex[i])
		{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
		dec += (atoi(&hex[i]) * pow(16.0,hold));
		break;

			case 'A':
			case 'a':
		dec += (10 * pow(16.0,hold));
		break;
			case 'B':
			case 'b':
		dec += (11 * pow(16.0,hold));
		break;
			case 'C':
			case 'c':
		dec += (12 * pow(16.0,hold));
		break;
					
			case 'D':
			case 'd':
		dec += (13 * pow(16.0,hold));
		break;
					
			case 'E':
			case 'e':
		dec += (14 * pow(16.0,hold));
		break;
			case 'F':
			case 'f':
		dec += (15 * pow(16.0,hold));
		break;

			default: printf(&quot;NOT HEX VALUE\n&quot;);
				 exit(1);
				 break;
		}

		hex[i] = 0;
		hold += 1;
	}

	printf(&quot;Hex is %s ... Decimal is %ld\n&quot;, b, dec);

        return 0;
}
 
Here is an overly simple way to do it:

#include <stdio.h>

int main() {
char *hexNum = &quot;69&quot;;
long intergerNum;
char c;

integerNum = strtol(hexNum, 0, 16);
printf(&quot;%d\n&quot;, integerNum); // Should print &quot;105&quot;
c = (char) integerNum;
printf(&quot;%c\n&quot;, c); // Should print &quot;i&quot;

return(1);
}

The prototype for strol is:

long strtol(char *s, char **end_ptr, int base);

The end_ptr is a pointer to where the function stops converting hex values (ie. when it find a non-hex character like 'L'). You can set that to NULL and be fine.

Hope this helps...
-Skatanic
 
Uhm... i have another question: this is the code that posted Skatanic:

#include <stdio.h>

int main() {
char *hexNum = &quot;69&quot;;
long intergerNum;
char c;

integerNum = strtol(hexNum, 0, 16);
printf(&quot;%d\n&quot;, integerNum); // Should print &quot;105&quot;
c = (char) integerNum;
printf(&quot;%c\n&quot;, c); // Should print &quot;i&quot;

return(1);
}

...how could i use it to convert a string of hex numbers???? for example if i start with char*hexNum=&quot;696F616F&quot; instead of char*hexNum=&quot;69&quot;
how could i?
 
That code should work with the hex numbers. strtol recognizes characters 0-9 and A-F when 16 is passed as the base.
 
yes...but it recognizes a range of hex numbers too...doesn't it? if i have char *hexNum=&quot;696F616F&quot;, with integerNum = strtol(hexNum, 0, 16); i pass hexNum in integerNum (that become &quot;0x696F616F&quot;)...ho can i pass this number to a char variable?
 
Do you mean that you want to convert &quot;696F616F&quot; into &quot;ioao&quot;?

If so you can modify the above code to:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char *hexNum = &quot;696F616F&quot;;
long integerNum;
char c;
int i;

for (i = 0; i < strlen(hexNum); i += 2) {
char *tmp = &quot; &quot;;
memcpy(tmp, hexNum + i, 2);
integerNum = strtol(tmp, 0, 16);
printf(&quot;%d\n&quot;, integerNum);
c = (char) integerNum;
printf(&quot;%c\n&quot;, c);
}

return(1);
}
 
how to convert ascii to OCTAL CODE?

I'm Keep Studying.... please show the way...
Not Good in English
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top