//10->2
CString DecimalToBin(int iDecimal){
char c[20];
_itoa(iDecimal, c, 2 );
CString str(c);
return str;
}
//10->16
CString DecimalToHex(int iDecimal)
{
char c[20];
sprintf(c, "%x", iDecimal);// _ltoa( l, buffer, 16 );
CString str(c);
return str;
}
//16->10
int HexToDecimal(CString strHex)
{
ULONG lValue;//4 BYTE
sscanf(strHex,"%X",&lValue);
return lValue;
}
//16->2
CString HexToBinary(CString strHex)
{
ULONG lValue;//4 BYTE
sscanf(strHex,"%X",&lValue);
char c[20];
_itoa(lValue, c, 2 );
CString str(c);
return str;
}
//2->10
UINT BinaryToDec(CString strBin)
{
// assert(strHex.GetLength<=8);
char *stopstring;
const char *cBin = (LPSTR)(LPCTSTR)strBin;
UINT iValue;
iValue =strtol(cBin, &stopstring, 2);
return iValue;
}
//2->16
CString BinaryToHex(CString strBin)
{
char *stopstring;
const char *cBin = (LPSTR)(LPCTSTR)strBin;
UINT iValue;
iValue =strtol(cBin, &stopstring, 2);
char c[20];
sprintf(c, "%x", iValue);
CString str(c);
return str;
}
可以按照自己的需求修改,没考虑周到,比如16->10函数只能转换4个字节。