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型,可以节省空间。
代码:
#include <iostream>
#include <cmath>
using namespace std; #define SIZE 100 bool BaseError = false; //考虑不同进制的转换
char * _itoa_1 (long int num,char str[],int base)
{
if ( !(base == || base == || base == ) )
{
BaseError = true;
return NULL;
} int digit[]; //保存各个数字
int cnt = ;
int sign = ;
bool PreFlag = false;
char *str_copy = str;
unsigned long int uNum = ; if (num < ) //对负数的处理
{
sign = -;
uNum = (unsigned long int) - num; //转换为unsigned long int,这样才能处理绝对值最大的负数
}
else
{
uNum = num;
} while (uNum)
{
digit[cnt++] = uNum % base;
uNum = uNum / base;
} --cnt; if (cnt >= && - == sign) //对负数,要加负号
{
*str++ = '-';
} while (cnt >= )
{
if ( == base) //对不同的进制,转换方式不同
{
*str++ = digit[cnt--] + '';
}
else if ( == base)
{
if (false == PreFlag)
{
*str++ = '';
*str++ = 'x';
PreFlag = true;
}
else
{
if (digit[cnt] >= && digit[cnt] <= )
{
*str++ = digit[cnt--] + '';
}
else
{
*str++ = digit[cnt--] - + 'A';
}
}
}
else if ( == base)
{
if (false == PreFlag)
{
*str++ = '';
PreFlag = true;
}
else
{
*str++ = digit[cnt--] + '';
}
}
} *str = '\0';
return str_copy; //返回指针地址
} //测试程序
int main()
{
char str[SIZE];
int num = ;
int base = ;
cout<<"test _itoa_1..."<<endl;
cout<<"please enter the integer number and the base:"<<endl;
while(cin>>num>>base)
{
cout<<"the integer number is :"<<num<<endl;
cout<<"the int number is : "<<_itoa_1(num,str,base)<<endl;
cout<<"please enter the string :"<<endl;
cout<<"please enter the integer number and the base:"<<endl;
} return ;
}
运行结果:
test _itoa_1...
please enter the integer number and the base: the integer number is :
the int number is :
please enter the string :
please enter the integer number and the base:
- the integer number is :-
the int number is : -
please enter the string :
please enter the integer number and the base: the integer number is :
the int number is : 0x22E6
please enter the string :
please enter the integer number and the base: the integer number is :
the int number is :
please enter the string :
please enter the integer number and the base:
- the integer number is :-
the int number is : -0x4E
please enter the string :
please enter the integer number and the base:
- the integer number is :-
the int number is : -
please enter the string :
please enter the integer number and the base:
^Z
请按任意键继续. . .
itoa函数的实现(不同进制)的更多相关文章
- itoa()、atoi()、任意进制转换
头文件:<stdlib.h> itoa --功能:将任意类型的数字转换为字符串.在<stdlib.h>中与之有相反功能的函数是atoi. atoi----功 能: 将字符串转换 ...
- PHP中进制之间的互相转换
常见的进制: 二进制 binary -----> bin 八进制 octal -----> oct 十进制 decimal -----> dec 十六进 ...
- Python 各种进制转换
#coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...
- 10进制与16进制之间的转换 delphi
delphi中有直接把10进制转换成16进制的函数: function IntToHex(Value: Integer; Digits: Integer): string; o ...
- python(42):进制转换
十六进制 到 十进制 使用 int() 函数 ,第一个参数是字符串 '0Xff' ,第二个参数是说明,这个字符串是几进制的数. 转化的结果是一个十进制数. >>> int('0xf ...
- C++实现进制转换
知识内容: 1.string类基本使用 2.10进制转2进制 3.10进制转8进制和10进制转16进制 4.上述3种转换的递归实现 注:进制的表示: 二进制:开头是0b,eg: 0b1011(注:c/ ...
- 解决nginx在记录post数据时 中文字符转成16进制的问题【转载】
1. 问题描述 nginx 在获取post数据时候,如果是中文,则转换成16进制显示在日志文件中,如下图所示. Paste_Image.png 日志格式为: log_format postdata ...
- Oracle中如何进行进制转换(2进制,10进制,16进制)
1.16进制转换为10进制 可以通过to_number函数实现 SQL> select to_number('19f','xxx') from dual; TO_NUMBER('19F','XX ...
- 轻松实现C/C++各种常见进制相互转换
其它进制转为十进制 在实现这个需求之前,先简单介绍一个c标准库中的一个函数: long strtol( const char *str, char **str_end, int base); 参数详细 ...
- 洛谷 题解 P1604 【B进制星球】
题目:P1604 B进制星球 本人提交记录:R6292872 作为一个极其无聊的人,我没事干地写了operator... 思路很简单: 读入b 读入b进制的x,y ans = x + y 输出ans ...
随机推荐
- Linux rar
http://www.vpsyou.com/2010/06/15/to-extract-rar-centos.html wget http://www.rarsoft.com/rar/rarlinux ...
- 手机网站中 限制图片宽度 JS图片等比例缩放
<script type="text/javascript"> $(function () { var w = $(".content-co").w ...
- app配置智能硬件的解决方案
随着越来越多的智能硬件产品上市,越来越多的硬件都戴上了智能的帽子,什么智能插座,智能音箱,智能称等等.凡是所谓的智能,都是通过wifi或者蓝牙来连接互联网,其中蓝牙也只能算是手机的附属品吧.主要还是硬 ...
- PHP将二进制文件存入数据库以及从数据库中读取二进制文件
<?php $file = 'abcd.sqlite'; mysql_connect('localhost','root','123456'); mysql_select_db('zblog') ...
- CCNP第二天 帧中继综合实验
实验题如图所示: 要求全网可达 R5为帧中继交换机 R6 和 R1之间为快速以太网接口 所使用的拓扑为CCNA标准版拓扑图,如下所示: -------------------------------- ...
- 四、mysql内置函数
.字符串函数 concat('a','b'); 字符串拼接函数 ,,"我是A我是B"): 从指定位置开始替换指定长度的指定数据(起步为1) lower() 转小写 upper() ...
- go语言实现线程池
话说真的好久没有写博客了,最近赶新项目,工作太忙了.这一周任务比较少,又可以随便敲敲了. 逛论坛的时候突发奇想,想用go语言实现一个线程池,主要功能是:添加total个任务到线程池中,线程池开启num ...
- 汽车之家, 比亚迪等成为开源数据库SSDB的用户
开源的 NoSQL 数据库 SSDB 已经一岁多了! 在这一年中, SSDB 不断被应用在众多业界知名互联网企业, 创业团队的产品中. 最近, 比亚迪汽车也成为 SSDB 的用户, 其将 SSDB 作 ...
- python多进程中的队列数据共享问题
0x00 起 今天在写一个小东西的时候,需要控制并发量,但又不能直接调用python multiprocessing(问题会在文后提到).于是尝试用Queue来实现. 最一开始的思路是这样的: fro ...
- submit和button提交表单的区别
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...