c++: How to convert a DWORD to CHAR*?

I'm trying to convert a DWORD (that is gauranteed to be populated with a numberic value) to a char*?  How would I go about doing this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

do you want to convert 1234567  to  "1234567"?


do you want to convert 0x1828342 to a  pointer to a character at address 0x1828342?

very different things.
i-mate05Author Commented:
I'm looking to convert 123456789 to "123456789".
int TheNum; char Out[1000];

TheNum = 123456789;

sprintf( Out, "%d", TheNum );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

i-mate05Author Commented:
Is there a way to get the actual size of the int so the char length is exact?
Perhaps something like

DWORD i = 123456789 ;
int size = int(i/10)+1 ;

This is primitive and there is probably a better way to find the number of numbers in a number...heh, but you could try this:

int length = 0;
int num = 1500;

while( num > 0 )
    num /= 10;
    length ++;

// length now equals 4

Dividing by ten is a good way, if a bit slow.

an alternative way, a bit faster but less precise, would be to divide by two (by bit shifting), then multiply the resulting count by log10(2),  or is it log2(10)?  

or just take ceiling( log10(num) ) + 1 and that shoul dbe close enough.

what about negative numbers and zero?

int x = crap;

bool y = x<0;
if ( y )
  length = ceiling( log10(num) ) + 2;
else if ( !x )
  length = 1;
  length = ceiling( log10(num) ) + 1;
>> Is there a way to get the actual size of the int so the char length is exact?
Use stringstream. You don't have to specify the size then.

#include <sstream>
#include <iostream>
DWORD dword = 123456789;
std::stringstream stream;
stream << dword;
std::string str = stream.str();
std::cout << str << std::endl;

If you want the char* then do this

char* cPtr = str.c_str();
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.