?
Solved

splitting digit hex codes into 2 chars

Posted on 2004-09-21
8
Medium Priority
?
245 Views
Last Modified: 2010-04-15
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
 
0
Comment
Question by:dublinsoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 172 total points
ID: 12116950
as hex is 0-9,A-F

so  if the high part of x1 is bigger then 9 like int x1 0xA1 you will get error output

so you should check it
for example

if(x1/16<10)
char1 = (x1/16)+'0';
else
char1 = (x1/16-10)+'A';
0
 
LVL 86

Expert Comment

by:jkr
ID: 12117007
What about

int x1=0x01;

printf("%2.2x", x1);

?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 164 total points
ID: 12117017
Or

char ac[2]
int x1=0x01;

snprintf(ac,2,"%2.2x", x1);

?

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12117021
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]);

0
 

Expert Comment

by:aakash_mandhar
ID: 12121202

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
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 164 total points
ID: 12122609
Just noticed mine and aakash won't work with 1 digit values. Must be:

void main()
{
int n=0x1;
char sol[3];                   // Buffer must have 3 bytes, for trailing null character
sprintf(sol,"%02X",n);    // Notice the %02X format, will ensure a leading 0 if number has 1 digit only.
printf("%c and %c are the 2 digits",sol[0],sol[1]);
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question