Solved

hex to char*...

Posted on 2004-10-13
5
541 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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