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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]);
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]);
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
int x1=0x01;
printf("%2.2x", x1);
?