// ConvNumsDlg.cpp : implementation file
// Download by 链接已屏蔽
#include "stdafx.h"
#include "ConvNums.h"
#include "ConvNumsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CConvNumsDlg dialog
CConvNumsDlg::CConvNumsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CConvNumsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CConvNumsDlg)
m_strDecimal = _T("");
m_strBinary = _T("");
m_strHex = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CConvNumsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConvNumsDlg)
DDX_Text(pDX, IDC_DECIMAL, m_strDecimal);
DDX_Text(pDX, IDC_BINARY, m_strBinary);
DDX_Text(pDX, IDC_HEX, m_strHex);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CConvNumsDlg, CDialog)
//{{AFX_MSG_MAP(CConvNumsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DECIMAL_CONV, OnDecimalConv)
ON_BN_CLICKED(IDC_BINARY_CONV, OnBinaryConv)
ON_BN_CLICKED(IDC_HEX_CONV, OnHexConv)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CConvNumsDlg message handlers
BOOL CConvNumsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CConvNumsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CConvNumsDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CConvNumsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//将16进制的一个字符转换为十进制的数
unsigned char CConvNumsDlg::BtoH(char ch)
{
//0-9
if (ch >= '0' && ch <= '9')
return (ch - '0');
//9-15
if (ch >= 'A' && ch <= 'F')
return (ch - 'A' + 0xA);
//9-15
if (ch >= 'a' && ch <= 'f')
return (ch - 'a' + 0xA);
return(255);
}
//转换十进制数
void CConvNumsDlg::OnDecimalConv()
{
UpdateData(TRUE);
//先转换为二进制
m_strBinary = DecimalToBinary(m_strDecimal);
//再转换为十六进制
m_strHex = BinaryToHex(m_strBinary);
UpdateData(FALSE);
}
//转换二进制数
void CConvNumsDlg::OnBinaryConv()
{
UpdateData(TRUE);
//转换为十进制
m_strDecimal = BinaryToDecimal(m_strBinary);
//转换为十六进制
m_strHex = BinaryToHex(m_strBinary);
UpdateData(FALSE);
}
//转换十六进制数
void CConvNumsDlg::OnHexConv()
{
UpdateData(TRUE);
//先转换为二进制
m_strBinary = HexToBinary(m_strHex);
//再转换为十进制
m_strDecimal = BinaryToDecimal(m_strBinary);
UpdateData(FALSE);
}
//转换十六进制为二进制
CString CConvNumsDlg::HexToBinary(CString strHex)
{
int nLenth = strHex.GetLength();
char* Hex = new char[nLenth];
Hex = strHex.GetBuffer(0);
CString strBinary = "";
for(int i=0;i
{
//转换一位十六进制数为十进制
char h = Hex[nLenth-1-i];
int j = BtoH(h);
CString str;
str.Format("%d",j);
//转换十进制为4为二进制
str = DecimalToBinary(str);
strBinary += str;
}
return strBinary;
}
//转换二进制为十六进制
CString CConvNumsDlg::BinaryToHex(CString strBinary)
{
int nLength = strBinary.GetLength();
CString str = strBinary;
//位数不是四的倍数时补齐
switch(nLength%4)
{
case 0:
break;
case 1:
strBinary.Format("%d%d%d%s",0,0,0,str);
break;
case 2:
strBinary.Format("%d%d%s",0,0,str);
break;
case 3:
strBinary.Format("%d%s",0,str);
break;
default:
return "";
break;
}
CString strHex,str1;
str1 = "";
nLength = strBinary.GetLength();
for(int i=1;i<=(nLength/4);i++)
{
//每四位二进制数转换为一十六进制数
str = strBinary.Left(4);
CString strDecimal = BinaryToDecimal(str);
int nDecimal = atoi(strDecimal.GetBuffer(0));
if(nDecimal<10)
str1.Format("%d",nDecimal);
else
{
char c = 'A' + (nDecimal-10);
str1.Format("%c",c);
}
strHex += str1;
strBinary = strBinary.Right(strBinary.GetLength()-str.GetLength());
}
return strHex;
}
//转换十进制为二进制
CString CConvNumsDlg::DecimalToBinary(CString strDecimal)
{
int nDecimal = atoi(strDecimal.GetBuffer(0));
int nYushu;//余数
int nShang;//商
CString strBinary = "";
char buff[2];
CString str = "";
BOOL bContinue = TRUE;
while(bContinue)
{
nYushu = nDecimal%2;
nShang = nDecimal/2;
sprintf(buff,"%d",nYushu);
str = strBinary;
strBinary.Format("%s%s",buff,str);
nDecimal = nShang;
if(nShang==0)
bContinue = FALSE;
}
return strBinary;
}
//转换二进制为十进制
CString CConvNumsDlg::BinaryToDecimal(CString strBinary)
{
int nLenth = strBinary.GetLength();
char* Binary = new char[nLenth];
Binary = strBinary.GetBuffer(0);
int nDecimal = 0;
for(int i=0;i
{
char h = Binary[nLenth-1-i];
char str[1];
str[0] = h;
int j = atoi(str);
for(int k=0;k
{
j=j*2;
}
nDecimal += j;
}
CString strDecimal;
strDecimal.Format("%d",nDecimal);
return strDecimal;
}
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有