Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DWORD to displayable String 2

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I've looked in both Ivor Horton's "Beginning Visual C++ 6" book and Lippman's "C++ Primer" (both great books) and haven't been able to figure out how to convert a value stored in a DWORD to a string (I believe this is the best format?) so that I can display it via either a cout command or display it within an MFC text box.

For example, let's say I have the following code:


------start of example code-------------
int main ()
{
DWORD dwTest; //declare 32bit unsigned integer
dwTest=2487924635; //initialize value of dwTest
//cout<<&quot;Here is the stored DWORD:&quot;<<?????<<endl;
return 0;
}
-------end of example code-----

I guess I could take each number, starting with the least significant digit, covert it to an ascii character and output it....but I'd prefer to reuse some existing code.

What is the best way?

thanks, /jerry

 
I think you need there
CString x;
DWORD c;
x.Format(&quot;%d&quot;,c);
yourTextBox.SetWindowText(x);//I not sure about
//SetWindowText, if it doesn't work see what is the
//function to set caption in MFC Ion Filipski
1c.bmp


filipski@excite.com
 
with a straight cout I think you can also do:

cout<<&quot;Here is teh DWORD: &quot;<<(LPCTSTR)CString(myDword)<<endl;

Matt
 
see also string streams (STL) what are compatible with many compillers than CString.
ostrsrteam x;
x<<myDword;
MessageBox(0,x.str(),&quot;&quot;,0); Ion Filipski
1c.bmp


filipski@excite.com
 
Hmmmm, close.....

I'm using WTL (windows template library) and the mapping function DDX_TEXT really wants a LPTSTR. So I guess what I really need to do is go from a DWORD to a LPTSTR.

The code sort of looks like this:

//declare 32bit ptr to a constant
LPTSTR m_nHDDVolume;
//mapping
BEGIN_DDX_MAP(CMainDlg)
DDX_TEXT(IDC_HDDVOLNO, m_nHDDVolume)
END_DDX_MAP()

//
//....various other items...
//
//push out dword to edit box
LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
DWORD dwVolumeSerialNo;
dwVolumeSerialNo=2487924635;
m_nHDDVolume=?? derived from DWORD conversion
DoDataExchange(FALSE, IDC_HDDVOLNO);


I thought I could just go from a DWORD to a STRING and feed the DDX Exchange a string...but it really wants a LPTSTR.

help?

/jerry
 
this is true for WTL/ATL:
#include<comutil.h>
......
_variant_t x = DWORD(2);
MessabeBox(0,(LPSTR)_bstr_t(x),&quot;see the dword&quot;,0); Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top