分类: C++编程心得2009-09-20 20:48 745人阅读 评论(0) 收藏 举报
二进制、十进制以及十六进制之间的相互转换
前面在网友的指导下总结了二进制转十六进制的方法,我从图书馆找资料的时候,看到了一篇文章:进制转换。它总结了二进制、十进制以及十六进制之间的相互转换,现把代码与网友分享。
//将进制的一个字符转换为十进制的数
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);
//转换十进制为为二进制
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<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;
}