c语⾔使⽤库函数的2、8、10、16进制转换
只⽤于⾮负整数
#include<stdio.h>//上个程序是从⼗进制到任意进制的转换,这个程序是⼆进制(Binary)⼋进制(Octonary)⼗进制(Decimalism)⼗六进制(Hexadecimal) #include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void Binary_To_Decimalism(char ba[])
{
int length = strlen(ba);
//printf("%d\n",length);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(2.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(2.0,length-1-index));
}
printf("%lld\n",decimalismtemp);
}
void Binary_To_Octonary(char ba[])
{
int length = strlen(ba);
int index;
血吸虫病的传播途径long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(2.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(2.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
Decimalism_To_Octonary(Dec);乐山大佛介绍
}
void Binary_To_Hexadecimal(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(2.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(2.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
Decimalism_To_Hexadecimal(Dec);
}
void Decimalism_To_Binary(char ba[])
{
int length = strlen(ba),index=0;
char Bin[40];
long long DecimalismNumber = atoi(ba),divide,remainder;
//printf("%lld\n",DecimalismNumber);
/*divide = DecimalismNumber;
while(divide>0)
{
remainder = divide%2;
Bin[index++] = remainder+'0';
divide/=2;
}
Bin[index]='\0';
printf("%s\n",Bin);
strcpy(ba,Bin);
length = strlen(Bin);
for(index=0;index<length;index++)Bin[index]=ba[length-index];*/ itoa(DecimalismNumber,Bin,2);
printf("%s\n",Bin);
}
void Decimalism_To_Octonary(char ba[])
{
int length = strlen(ba);
char Oct[40];
long long DecimalismNumber = atoi(ba);
//printf("%lld\n",DecimalismNumber);
itoa(DecimalismNumber,Oct,8);
printf("%s\n",Oct);
}
void Decimalism_To_Hexadecimal(char ba[])
{
int length = strlen(ba);
char Hex[40];
long long DecimalismNumber = atoi(ba);
itoa(DecimalismNumber,Hex,16);
printf("%s\n",Hex);
}
void Octonary_To_Binary(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(8.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(8.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
Decimalism_To_Binary(Dec);
}
void Octonary_To_Decimalism(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(8.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(8.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
printf("%s\n",Dec);
}
void Octonary_To_Hexadecimal(char ba[])
{
师德考核个人总结
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(8.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(8.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
Decimalism_To_Hexadecimal(Dec);
}
void Hexadecimal_To_Binary(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(16.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(16.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
Decimalism_To_Binary(Dec);
}
void Hexadecimal_To_Octonary(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(16.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(16.0,length-1-index));
}
char Dec[40];
梦见生了一个男孩itoa(decimalismtemp,Dec,10);
Decimalism_To_Octonary(Dec);
}
void Hexadecimal_To_Decimalism(char ba[])
{
int length = strlen(ba);
int index;
long long decimalismtemp = 0;
for(index=0;index<length;index++)
{
decimalismtemp += (ba[index]-'0')*(int)pow(16.0,length-1-index);
//printf("%d\n",(ba[index]-'0')*(int)pow(16.0,length-1-index));
}
char Dec[40];
itoa(decimalismtemp,Dec,10);
printf("%s\n",Dec);
}
int main()
{
char origin[40];
int from,to;
printf("\t这个程序仅⽀持2、8、10、16进制之间的相互转换\n");
printf("输⼊要转换的数: ");
scanf("%s",&origin);
printf("这个数的进制?: ");
scanf("%d",&from);
printf("要转换为的进制?: ");
scanf("%d",&to);
switch(from)
{
ca 2:
{
switch(to)
{
ca 8:Binary_To_Octonary(origin);break;
ca 10:Binary_To_Decimalism(origin);break;
红豆的功效与作用
ca 16:Binary_To_Hexadecimal(origin);break;
}
break;
}
ca 8:
{
switch(to)
{
ca 2:Octonary_To_Binary(origin);break;
ca 10:Octonary_To_Decimalism(origin);break;
ca 16:Octonary_To_Hexadecimal(origin);break;
}
break;
}
ca 10:
开工报告{
switch(to)
{
ca 8:Decimalism_To_Octonary(origin);break;
ca 2:Decimalism_To_Binary(origin);break;
ca 16:Decimalism_To_Hexadecimal(origin);break;
}
break;
}
ca 16:
{
switch(to)
{
ca 8:Hexadecimal_To_Octonary(origin);break;
ca 10:Hexadecimal_To_Decimalism(origin);break;
ca 2:Hexadecimal_To_Binary(origin);break;
}
break;
}
}
return0;幻想童话
}
不使⽤itoa或atoi的10进制向任意进制转换
#include <stdio.h>
void system_conv(char ss[], int n, int m){
char s[33];
int k = 0, t, i, j = 0;
if ( n == 0 ){ //当数为0,字符串直接是0
ss[0] = '0';
ss[1] = '\0'; //给出字符串的结尾
return;
}
if ( n < 0 ){ //负数字符串开头要加负号
ss[j++] = '-';
n = -n;//转绝对值才能⽤
}
while ( n > 0 ){
t = n%m;//取余数并⽣成序列
s[k++] = t>9 ? t-10+'A' : t+'0';//根据余数的值赋字符,若余数⼤于9说明⽬标进制是10+,采⽤字母表⽰⼀位 n /= m;//记得要使n变化,退出条件是n到0
}
for ( i = k-1; i>=0; --i ){ //将之前⽣成的字符串逆置
ss[j++] = s[i];
拔牙后多久可以正常吃饭}
ss[j] = '\0'; //给出字符串的结尾,防⽌由于输出长度产⽣错误
}
int main(){
char s[33];
int n, m; //n是⼗进制数,m是转换的⽬标进制
while ( ~scanf("%d%d", &n, &m) ){
system_conv(s, n, m);
printf("%s\n", s);
}
return0;
}