将整数转换为10进制、2进制、16进制的数字串
来源:互联网 发布:mac磁盘工具抹掉失败 编辑:程序博客网 时间:2024/05/16 19:37
将整数转换为10进制、2进制、16进制的数字串,采用函数调用方法。
#include <stdio.h>
void to_binary(int n); //将整数转换为二进制
void to_dec(int n); //将整数转换为十进制
void to_hex(int n); //将整数转换为十六进制
int main()
{
int a;
printf("Please enter a integer number(q to quit):\n");
while(scanf("%d",&a) == 1 )
{
printf("binary output:");
to_binary(a);
printf("\n");
printf("dec output:");
to_dec(a);
putchar('\n');
printf("hex output:");
to_hex(a);
putchar('\n');
}
return 0;
}
void to_binary(int n)
{
int i;
i = n % 2;
if(n >= 2)
to_binary(n / 2); //递归,反序输出
putchar('0'+ i ); //加‘ 0’,以字符型输出
return ;
}
void to_dec(int n)
{
int i;
i = n % 10;
if( n >= 10 )
to_dec( n / 10);
putchar('0' + i);
return ;
}
void to_hex(int n)
{
int i;
i = n % 16;
if( n >= 16 )
to_hex( n / 16);
if( i > 9)
putchar('0' + i + 7); //超过9的数字输出字母,如A、B、C等等,将7换为39可以小写输出
else
putchar('0' + i);
return ;
}
示例输出:
Please enter a integer number(q to quit):
12
binary output:1100
dec output:12
hex output:C
78
binary output:1001110
dec output:78
hex output:4E
q
- 将整数转换为10进制、2进制、16进制的数字串
- 将整数转换为10进制、2进制、16进制的数字串
- 将整数转换为10进制、2进制、16进制的数字串
- 将16进制字符串转换为整数的函数
- 将整数n转换为以b进制的数
- 将8进制字符串转换为整数的代码
- 编写一个函数itob(),将整数n转换为以b进制的数,保存到s中
- strtol----将16进制字符串转换为整数
- 将16进制字符串转换为整数输出
- 将任意进制(2~36)的数转换为10进制,参数k=16就是16进制转10进制
- 将10进制转换为2-16进制
- 将十进制的一个数转换为n进制
- 将十进制转换为任意进制的数
- 将十进制数转换为2进制、8进制和16进制的程序。要求通过继承Frame实现该窗口
- C语言-进制转换-输入一个字节的整数(0~255),然后将该数的转换为二进制数,然后将奇数位和偶数位互换
- 编写一个函数itob(int n,char s[], int b),将整数n转换为以b进制的数。保存到s中。
- C语言将16进制的数转换为字符串的方法
- C语言将16进制的数转换为字符串的方法(改进)
- sqoop 1.4
- hadoop streaming 坑
- 说在前面
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- PTAM增强现实相关
- 将整数转换为10进制、2进制、16进制的数字串
- C++ Primer_10 泛型算法
- 2017 网易有道内推
- 水的红外振动光谱的模拟
- 特别的Java构造函数
- 校园故事-一个非典型工科男的电子设计之路(整理版)
- [Tkinter 教程01] 入门: Label 控件
- [POJ2406] Power Strings
- <<UNIX环境高级编程>>之第八章理解