Solved

hex to char*...

Posted on 2004-10-13
5
539 Views
Last Modified: 2010-10-05
Hello,
when I print the following,
      printf("\t%02x00\t",(const char *)test[0]);
      printf("\t%02x00\t",(const char *)test[1]);
      printf("\t%02x00\t",(const char *)test[2]);
...
I have this output:    7500            0c00            7800 ....
I want it in the following form: 750c78....
I found a method by setting the cmd-line position allways two placs back, but I think that there should be a more elegant method; does anyone knows a eleganter method??

Thanks a lot.
0
Comment
Question by:Confettis
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12299112
printf ("%02x%02x%02x", (const char*) test[0], (const char*) test[0], (const char*) test[0])
0
 
LVL 13

Expert Comment

by:SteH
ID: 12299136
Or if you need to put it in a loop use

 printf ("%02x", (const char*) test[i]);

x means hex display, 02 means 2 digits left filled with 0 to fit the width (of 2)

Your format was
\t: a tab
%02x: as above
00: "00" string constant with two zeroes.
\t: and a tab again.
0
 

Author Comment

by:Confettis
ID: 12299251
thanks a lot. It works fine with the second method.
(Do you also happen to know how I can convert a hex saved in a char* to a char* ?
Example:
char * hexa="616263...."
char texta[10];
texta// here "abc..." should be saved...)

I found that it is easily done with a CString, but I don't want to use MFC...
If you don't know it I'll open a new thread...

Thanks .
0
 
LVL 13

Accepted Solution

by:
SteH earned 125 total points
ID: 12299298
for (int i=0;i<10;++i) {
   sscanf (&hexa[i*2], "%02x", &texta[i]);
}

is one way. There are others, like sstreams
0
 

Author Comment

by:Confettis
ID: 12299403
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now