C语言 进制间转换

#include<stdio.h>
#include<string.h>

void decimalism_system_convert_to_other_system(unsigned int data, unsigned int system, char *str)
{
char a[20] = { 0 };
int i = 0;
int len = 0;
for (i = 0; data > (system - 1); i++)
{
str[i] = data % system + '0';
data = data / system;
}
str[i] = data + '0';


    len = i+1;
char temp;
for (i = 0; i < len / 2; i++)
{
temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
printf("str is:%s\n", str);

}


unsigned int other_system_convert_to_decimalism_system(unsigned int system, char * str)
{
int m;
int lenb;
unsigned int data = 0;


while (*str != '\0')
{
data *= system;
data += (*str - '0');
str++;
}
return data;

printf("data is:%d\n", data);

}

void main(void)
{
unsigned int data = 123456;
char str[100] = {0};
decimalism_system_convert_to_other_system(data, 7, str);
printf("str main is:%s\n", str);
data = other_system_convert_to_decimalism_system(7, str);
printf("data main is:%d\n", data);
system("pause");
}

阅读更多
文章标签: C语言 进制转换
个人分类: 进制转换 C语言
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭