# 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?

Thanks.
###### 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.

Commented:
do you want to convert 1234567  to  "1234567"?

or

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

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

TheNum = 123456789;

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

Experts Exchange Solution brought to you by

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

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

DWORD i = 123456789 ;
int size = int(i/10)+1 ;
0
Commented:
Hi,

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

Regards,
choo_chu
0
Commented:
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.

0
Commented:
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;
else
length = ceiling( log10(num) ) + 1;
0
Commented:
>> 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();
0
###### 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
C++

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.