Click to See Complete Forum and Search --> : Converting DWORD to CString


Andrew Truckle
June 18th, 1999, 10:13 AM
I have a DWORD variable and want to convert to a CString.

Likewise, I would like to convert a CString to a DWORD.

I know how to do it for other variables using functions like

[code]
number = atoi(str); //string to number
str.Format("%d", number); //number to string
[/ccode]

I would appreciate any suggestions and example code.

Lee N.
June 18th, 1999, 11:06 AM
Try this.


CString MyString;
DWORD SomeNum;
MyString.Format ("%d", SomeNum);




Lee

Andrew Truckle
June 18th, 1999, 11:15 AM
surely %d is a normal interger, not a DWORD?
And what about string to DWORD?

Lee N.
June 18th, 1999, 11:22 AM
Sorry, you should use "%u" instead of "%d" since "%u" is for unsigned ints (which is a DWORD). As far as converting from DWORD to string, I do not know of any unsigned int versions of atoi().

Lee

Andrew Truckle
June 18th, 1999, 11:33 AM
Yes - %u

And I thought about it - I could use sscanf to convert from string to DWORD:

[code]
DWORD var;
CString MyStr = "1234";
sscanf(MyStr, "%u", &var);
[/ccode]

I think that would work ok.

Lee N.
June 18th, 1999, 11:41 AM
Actually, there is a function called strtoul, which converts strings to unsigned longs.


unsigned long strtoul( const char *nptr, char **endptr, int base );





Lee