VC++常用的进制转换程序代码分享

分享一个VC++常用的进制转换程序,这是其中主要文件的代码,在这个文件中,可实现 二进制 十六进 十进制以及余数、商的计算等,在此代码基础上,你可开发出完整的进制转换程序
//将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<nLenth;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 = "";// www.codesc.net
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<nLenth;i++)
{
char h = Binary[nLenth-1-i];
char str[1];
str[0] = h;
int j = atoi(str);
for(int k=0;k<i;k++)
{
j=j*2;
}
nDecimal += j;
}
CString strDecimal;
strDecimal.Format("%d",nDecimal);
return strDecimal;
}
原文:http://www.codesc.net/tech/3692.shtml

posted @ 2018-10-08 23:47  jianghuluanke  阅读(326)  评论(0编辑  收藏  举报