Solved

hex to char*...

Posted on 2004-10-13
5
538 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
Comment Utility
printf ("%02x%02x%02x", (const char*) test[0], (const char*) test[0], (const char*) test[0])
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

9 Experts available now in Live!

Get 1:1 Help Now