itoa函数的实现(不同进制)
2013-07-08 17:12:30
itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面。
小结:
一下几点需要考虑:
- 对负数,要加上负号;
- 考虑不同进制,根据要求进行处理;对不同的进制转换方法不同(如对于16进制还要考虑10~15的情况:*str++ = digit[cnt--] - 10 + 'A');
- 最后要加上字符串结束符*str = '\0';
- 要正确输出绝对值最大的负数,就必须将输入转换为无符号数,因为对下面的代码:
long int num = -2147483648;
cout<<-num<<endl;
执行结果为:-2147483648
因为-2147483648取负号,为2147483648,但对于32位的long int型,其所能表示的最大正数为2147483647,因此2147483648将转换为-2147483648,在执行下面的while循环时就会得到意想不到的怪异结果。
对16进制,要加上前缀:
if (false == PreFlag)
{
*str++ = '0';
*str++ = 'x';
PreFlag = true;
}
最8进制,同样:
if (false == PreFlag)
{
*str++ = '0';
PreFlag = true;
}
下面代码中的数组digit还可以进行优化,如类型改为char型,可以节省空间。
代码:
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 #define SIZE 100 6 7 bool BaseError = false; 8 9 //考虑不同进制的转换 10 char * _itoa_1 (long int num,char str[],int base) 11 { 12 if ( !(base == 10 || base == 16 || base == 8) ) 13 { 14 BaseError = true; 15 return NULL; 16 } 17 18 int digit[20]; //保存各个数字 19 int cnt = 0; 20 int sign = 1; 21 bool PreFlag = false; 22 char *str_copy = str; 23 unsigned long int uNum = 0; 24 25 if (num < 0) //对负数的处理 26 { 27 sign = -1; 28 uNum = (unsigned long int) - num; //转换为unsigned long int,这样才能处理绝对值最大的负数 29 } 30 else 31 { 32 uNum = num; 33 } 34 35 while (uNum) 36 { 37 digit[cnt++] = uNum % base; 38 uNum = uNum / base; 39 } 40 41 --cnt; 42 43 if (cnt >= 0 && -1 == sign) //对负数,要加负号 44 { 45 *str++ = '-'; 46 } 47 48 while (cnt >= 0) 49 { 50 if (10 == base) //对不同的进制,转换方式不同 51 { 52 *str++ = digit[cnt--] + '0'; 53 } 54 else if (16 == base) 55 { 56 if (false == PreFlag) 57 { 58 *str++ = '0'; 59 *str++ = 'x'; 60 PreFlag = true; 61 } 62 else 63 { 64 if (digit[cnt] >= 0 && digit[cnt] <= 9) 65 { 66 *str++ = digit[cnt--] + '0'; 67 } 68 else 69 { 70 *str++ = digit[cnt--] - 10 + 'A'; 71 } 72 } 73 } 74 else if (8 == base) 75 { 76 if (false == PreFlag) 77 { 78 *str++ = '0'; 79 PreFlag = true; 80 } 81 else 82 { 83 *str++ = digit[cnt--] + '0'; 84 } 85 } 86 } 87 88 *str = '\0'; 89 return str_copy; //返回指针地址 90 } 91 92 //测试程序 93 int main() 94 { 95 char str[SIZE]; 96 int num = 0; 97 int base = 0; 98 cout<<"test _itoa_1..."<<endl; 99 cout<<"please enter the integer number and the base:"<<endl; 100 while(cin>>num>>base) 101 { 102 cout<<"the integer number is :"<<num<<endl; 103 cout<<"the int number is : "<<_itoa_1(num,str,base)<<endl; 104 cout<<"please enter the string :"<<endl; 105 cout<<"please enter the integer number and the base:"<<endl; 106 } 107 108 return 0; 109 }
运行结果:
test _itoa_1... please enter the integer number and the base: 2147364748 10 the integer number is :2147364748 the int number is : 2147364748 please enter the string : please enter the integer number and the base: -2147364749 10 the integer number is :-2147364749 the int number is : -2147364749 please enter the string : please enter the integer number and the base: 8934 16 the integer number is :8934 the int number is : 0x22E6 please enter the string : please enter the integer number and the base: 78 8 the integer number is :78 the int number is : 0116 please enter the string : please enter the integer number and the base: -78 16 the integer number is :-78 the int number is : -0x4E please enter the string : please enter the integer number and the base: -78 8 the integer number is :-78 the int number is : -0116 please enter the string : please enter the integer number and the base: ^Z 请按任意键继续. . .
转载于:https://www.cnblogs.com/youngforever/p/3178461.html
itoa函数的实现(不同进制)相关推荐
- php源码十六进制加密,php-简单对称加密算法和字符串与十六进制之间的互转函数,php-十六进制_PHP教程...
php-简单对称加密算法和字符串与十六进制之间的互转函数,php-十六进制 /** * 简单对称加密算法之加密 * @param String $string 需要加密的字串 * @param Str ...
- c语言CString转数字函数,CString与16进制的CByteArray之间相互转化
//将单个字符转为相应的16进制数,否则返回16 char Hex2Char(char ch) { if((ch>='0')&&(ch<='9')) return ch ...
- python内置函数bin,Python内置函数bin() oct()等实现进制转换
Python内置函数bin() oct()等实现进制转换 使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: ...
- 8. 进制转化的函数
一,表示进制的单词 bin:二进制 oct:八进制 dec:十进制 hex:十六进制 二,四种进制的数据表示方式 $bi ...
- c语言实现进制转换器
在了解了c语言中atoi和itoa函数以后,分别对其进行了实现.并以这两个函数为基础编写了一个任意进制的相互转换器.过程可分为两个步骤,先将任意进制的字符串转利用atoi函数换成10进制数,再将10进 ...
- 10进制与16进制之间的转换 delphi
delphi中有直接把10进制转换成16进制的函数: function IntToHex(Value: Integer; Digits: Integer): string; o ...
- hexde php_怎样在PHP中把16进制HEX数据转换为2进制数据呢?
展开全部 十六进制62616964757a686964616fe78988e69d8331333337623531转为二进制有两种理解方法,第一是十六进制字符串转二进制字符串,第二是十六进制数转二进制 ...
- 10进制与16进制之间的转换 delphi
delphi中有直接把10进制转换成16进制的函数: function IntToHex(Value: Integer; Digits: Integer): string; o ...
- python数据结构:进制转化探索
*********************************第一部分*************************************************************** ...
- 【Java】进制转换器的实现
import java.util.Scanner;public class BHDConverter {static int from; //全局变量 从几进制转换为几进制static int to; ...
最新文章
- XAMPP环境下配置Phalcon框架
- java中string的方法_java中String类中的常用方法
- 【luogu4408】 [NOI2003]逃学的小孩 [动态规划 树的直径]
- Monte Carlo Rendering De-noising
- 金融数据获取的api接口
- 深入理解JS对象和原型链
- cmd命令打开文本文档_Windows常用CMD网络命令
- 单设施布置方法-精确重心法
- oracle的权限授予,oracle权限命令
- 数字化改革是逼不得已,最后却帮我提高了20%生产效率
- cad绘制正八边形_CAD怎么画正八边形 看完你学会了么
- 微信小程序开发之mpVue
- 详解Win10家庭版/专业版/企业版功能区别
- object sender,EventArgs e
- 消费者物价指数CPI
- exe4j工具使jar包生成exe可执行文件
- 卫宁、智云的医疗SaaS出路:纵深、拓宽
- 商品搜索引擎--商品推荐
- 对话李志飞:出门问问最早将于2019年底考虑上市
- NPOI合并单元格后边框显示不正确?
热门文章
- 利用 Pandas 将数据集中的某列文本拆分为多行
- 在项目中迁移MS SQLServer到Mysql数据库,实现MySQL数据库的快速整合
- 人工智能科学家发现 “衰老时钟”,我们的生物钟可以倒转
- The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)
- phpstudy apache配置https
- 为品牌管理增加检索名称和状态项
- 解决docker(ERROR: No such container: )错误
- linux mpstat命令
- linux下的PHP+Mysql+PHPWind实现
- vs2013常用快捷键