Link to home
Start Free TrialLog in
Avatar of dublinsoft
dublinsoft

asked on

splitting digit hex codes into 2 chars

Hi i am trying to convert the two digit hex value of unprintable chars into two seperate chars, then i can print them as part of a normal C-String

I Have tried

char char1, char2;
int x1 0x01;

char1 = (x1/16)+'0';
char2 = (x1%16)+'0';

printf("%c %c", char1, char2);

Ok where am I going wrong. Please help
 
ASKER CERTIFIED SOLUTION
Avatar of tzxie2000
tzxie2000
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
What about

int x1=0x01;

printf("%2.2x", x1);

?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can use standard itoa() function:

char hex[3];
int x1 = 0x01;  /* there is a missing = sign in your example */

itoa (x1, hex, 16);

/* now your digits are hex[0] and hex[1] */
printf("%c %c", hex[0], hex[1]);

Avatar of aakash_mandhar
aakash_mandhar


void main()
{
int n=0xAB;
char sol[2];
sprintf(sol,"%x",n);
printf("%c and %c are the 2 digits",sol[0],sol[1]);
}

PS> Hope this helps

Regards
Aakash
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial