整数与其他进制数之间的转换
2015-10-22 15:46
246 查看
编写一个函数itob(int n,char s[], int b),将整数n转换为以b进制的数。保存到s中。 #include <stdio.h> void reverse(char*left, char *right) { while(left < right) { chartmp = *left; *left = *right; *right = tmp; left++; right--; } } void itob(int n, char s[], int b) { char*start; char*end; start = s; while(n) { if (b <= 10) *s = (n %b) + '0'; else if (b == 16) *s = "0123456789abcdef"[n % b]; s++; n /= b; } *s = '\0'; end = s - 1; reverse(start, end); } int main() { int num= 1234; chararr[20]; int b =0; scanf("%d",&b); itob(num, arr, b); printf("%s\n",arr); system("pause"); return0; }
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- PowerShell函数简明教程