Solved

hex to char*...

Posted on 2004-10-13
5
546 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
[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
  • 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

Technology Partners: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

751 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