## CSDN博客

### 进制转换

1.

./*

*/
#include
#include
void fun( int m, int k )
{
int aa[20], i;
for( i = 0; m; i++ )
{
/**********found**********/
aa[i] = m%k;
m /= k;
}
for( ; i; i-- )
/**********found**********/
printf( "%d", aa[ i-1 ] );
//printf( "%d", aa[ i ] );
}

main()
{
int b, n;
clrscr();
printf( "/nPlease enter a number and a base:/n" );
scanf( "%d %d", &n, &b );
fun( n, b );
printf("/n");
}

2.char result [33];
result[32] = '/0';
for (int i = 0, j = 1; i < 33; i++)
{
result[32 - i - 1] = (nSource & j) ? '1' : '0';
j = j << 1;
}

3.unsigned char BtoH(char ch)//将16进制的一个字符转换为十进制的数
{
//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);
}

4.//转换十进制为二进制
CString 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;
}

char str[100];
itoa(11, str, 2);//根据最后itoa最后一个参数设置进制，可把十进制转换为其他进制

0 0