2012年3月14日 星期三

[VC(Visual C++) ]- 將字元透過CString和itoa()轉換成數字字串程式

[VC(Visual C++) ]- 將字元透過CStringitoa()轉換成數字字串程式



本篇分享字元透過CStringitoa()轉換成數字字串程式範例有興趣的(C/P)同好歡迎來http://filemarkets.com/file/jashliao/a9006d21/索取因為我不會上傳檔案分享哈哈 ^ ^












主要程式碼



001    // Ascll2StrIntDlg.cpp : implementation file                

002    //                

003                    

004    #include "stdafx.h"                

005    #include "Ascll2StrInt.h"                

006    #include "Ascll2StrIntDlg.h"                

007    #include <stdlib.h>                

008    #ifdef _DEBUG                

009    #definenew DEBUG_NEW                

010    #undef THIS_FILE                

011    staticchar THIS_FILE[] = __FILE__;                

012    #endif                

013                    

014    /////////////////////////////////////////////////////////////////////////////                

015    // CAboutDlg dialog used for App About                

016                    

017    class CAboutDlg : public CDialog                

018    {                

019    public:                

020        CAboutDlg();            

021                    

022    // Dialog Data                

023        //{{AFX_DATA(CAboutDlg)            

024        enum { IDD = IDD_ABOUTBOX };            

025        //}}AFX_DATA            

026                    

027        // ClassWizard generated virtual function overrides            

028        //{{AFX_VIRTUAL(CAboutDlg)            

029        protected:            

030        virtualvoid DoDataExchange(CDataExchange* pDX);    // DDX/DDV support            

031        //}}AFX_VIRTUAL            

032                    

033    // Implementation                

034    protected:                

035        //{{AFX_MSG(CAboutDlg)            

036        //}}AFX_MSG            

037        DECLARE_MESSAGE_MAP()            

038    };                

039                    

040    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)                

041    {                

042        //{{AFX_DATA_INIT(CAboutDlg)            

043        //}}AFX_DATA_INIT            

044    }                

045                    

046    void CAboutDlg::DoDataExchange(CDataExchange* pDX)                

047    {                

048        CDialog::DoDataExchange(pDX);            

049        //{{AFX_DATA_MAP(CAboutDlg)            

050        //}}AFX_DATA_MAP            

051    }                

052                    

053    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)                

054        //{{AFX_MSG_MAP(CAboutDlg)            

055            // No message handlers        

056        //}}AFX_MSG_MAP            

057    END_MESSAGE_MAP()                

058                    

059    /////////////////////////////////////////////////////////////////////////////                

060    // CAscll2StrIntDlg dialog                

061                    

062    CAscll2StrIntDlg::CAscll2StrIntDlg(CWnd* pParent /*=NULL*/)                

063        : CDialog(CAscll2StrIntDlg::IDD, pParent)            

064    {                

065        //{{AFX_DATA_INIT(CAscll2StrIntDlg)            

066        m_str1 = _T("");            

067        m_str2 = _T("");            

068        //}}AFX_DATA_INIT            

069        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32            

070        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);            

071    }                

072                    

073    void CAscll2StrIntDlg::DoDataExchange(CDataExchange* pDX)                

074    {                

075        CDialog::DoDataExchange(pDX);            

076        //{{AFX_DATA_MAP(CAscll2StrIntDlg)            

077        DDX_Text(pDX, IDC_EDIT1, m_str1);            

078        DDX_Text(pDX, IDC_EDIT2, m_str2);            

079        //}}AFX_DATA_MAP            

080    }                

081                    

082    BEGIN_MESSAGE_MAP(CAscll2StrIntDlg, CDialog)                

083        //{{AFX_MSG_MAP(CAscll2StrIntDlg)            

084        ON_WM_SYSCOMMAND()            

085        ON_WM_PAINT()            

086        ON_WM_QUERYDRAGICON()            

087        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)            

088        ON_BN_CLICKED(IDC_BUTTON2, OnButton2)            

089        //}}AFX_MSG_MAP            

090    END_MESSAGE_MAP()                

091                    

092    /////////////////////////////////////////////////////////////////////////////                

093    // CAscll2StrIntDlg message handlers                

094                    

095    BOOL CAscll2StrIntDlg::OnInitDialog()                

096    {                

097        CDialog::OnInitDialog();            

098                    

099        // Add "About..." menu item to system menu.            

100                    

101        // IDM_ABOUTBOX must be in the system command range.            

102        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);            

103        ASSERT(IDM_ABOUTBOX < 0xF000);            

104                    

105        CMenu* pSysMenu = GetSystemMenu(FALSE);            

106        if (pSysMenu != NULL)            

107        {            

108            CString strAboutMenu;        

109            strAboutMenu.LoadString(IDS_ABOUTBOX);        

110            if (!strAboutMenu.IsEmpty())        

111            {        

112                pSysMenu->AppendMenu(MF_SEPARATOR);    

113                pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);    

114            }        

115        }            

116                    

117        // Set the icon for this dialog.  The framework does this automatically            

118        //  when the application's main window is not a dialog            

119        SetIcon(m_hIcon, TRUE);            // Set big icon

120        SetIcon(m_hIcon, FALSE);        // Set small icon    

121                    

122        // TODO: Add extra initialization here            

123                    

124        return TRUE;  // return TRUE  unless you set the focus to a control            

125    }                

126                    

127    void CAscll2StrIntDlg::OnSysCommand(UINT nID, LPARAM lParam)                

128    {                

129        if ((nID & 0xFFF0) == IDM_ABOUTBOX)            

130        {            

131            CAboutDlg dlgAbout;        

132            dlgAbout.DoModal();        

133        }            

134        else            

135        {            

136            CDialog::OnSysCommand(nID, lParam);        

137        }            

138    }                

139                    

140    // If you add a minimize button to your dialog, you will need the code below                

141    //  to draw the icon.  For MFC applications using the document/view model,                

142    //  this is automatically done for you by the framework.                

143                    

144    void CAscll2StrIntDlg::OnPaint()                 

145    {                

146        if (IsIconic())            

147        {            

148            CPaintDC dc(this); // device context for painting        

149                    

150            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);        

151                    

152            // Center icon in client rectangle        

153            int cxIcon = GetSystemMetrics(SM_CXICON);        

154            int cyIcon = GetSystemMetrics(SM_CYICON);        

155            CRect rect;        

156            GetClientRect(&rect);        

157            int x = (rect.Width() - cxIcon + 1) / 2;        

158            int y = (rect.Height() - cyIcon + 1) / 2;        

159                    

160            // Draw the icon        

161            dc.DrawIcon(x, y, m_hIcon);        

162        }            

163        else            

164        {            

165            CDialog::OnPaint();        

166        }            

167    }                

168                    

169    // The system calls this to obtain the cursor to display while the user drags                

170    //  the minimized window.                

171    HCURSOR CAscll2StrIntDlg::OnQueryDragIcon()                

172    {                

173        return (HCURSOR) m_hIcon;            

174    }                

175                    

176    void CAscll2StrIntDlg::OnButton1()                 

177    {                

178        // TODO: Add your control notification handler code here            

179        char *pstr1;            

180        char chrdata[3];            

181        int j=0;            

182        int intdata=0;            

183        int intLen=0;            

184        UpdateData(true);            

185        //m_str2=m_str1;            

186        m_str2="";            

187        intLen=m_str1.GetLength();             

188        pstr1=m_str1.GetBuffer(0);            

189        for(j=0;j<intLen;j++)            

190        {            

191            intdata=*(pstr1+j);        

192            itoa(intdata,chrdata,10);        

193            m_str2+=chrdata;        

194            m_str2+=",";        

195        }            

196        UpdateData(false);            

197                    

198    }                

199                    

200    void CAscll2StrIntDlg::OnButton2()                 

201    {                

202        // TODO: Add your control notification handler code here            

203        char chr1;            

204        chr1=33;            

205        UpdateData(true);            

206        m_str1=chr1;            

207        UpdateData(false);            

208    }                

 



 


沒有留言:

張貼留言