c语言使用库函数的2、8、10、16进制转换

更新时间:2023-07-26 19:56:17 阅读: 评论:0

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;
}

本文发布于:2023-07-26 19:56:17,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1118737.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   进制   变化   转换   途径   考核   红豆
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图