2013-07-08 17:12:30

itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面。

小结:

一下几点需要考虑:

  1. 对负数,要加上负号;
  2. 考虑不同进制,根据要求进行处理;对不同的进制转换方法不同(如对于16进制还要考虑10~15的情况:*str++ = digit[cnt--] - 10 + 'A');
  3. 最后要加上字符串结束符*str = '\0';
  4. 要正确输出绝对值最大的负数,就必须将输入转换为无符号数,因为对下面的代码:

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函数的实现(不同进制)的更多相关文章

  1. itoa()、atoi()、任意进制转换

    头文件:<stdlib.h> itoa --功能:将任意类型的数字转换为字符串.在<stdlib.h>中与之有相反功能的函数是atoi. atoi----功 能: 将字符串转换 ...

  2. PHP中进制之间的互相转换

    常见的进制: 二进制   binary   ----->  bin 八进制   octal     ----->  oct 十进制   decimal ----->  dec 十六进 ...

  3. Python 各种进制转换

    #coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...

  4. 10进制与16进制之间的转换 delphi

    delphi中有直接把10进制转换成16进制的函数: function   IntToHex(Value:   Integer;   Digits:   Integer):   string;   o ...

  5. python(42):进制转换

    十六进制 到 十进制 使用 int() 函数 ,第一个参数是字符串 '0Xff' ,第二个参数是说明,这个字符串是几进制的数.  转化的结果是一个十进制数. >>> int('0xf ...

  6. C++实现进制转换

    知识内容: 1.string类基本使用 2.10进制转2进制 3.10进制转8进制和10进制转16进制 4.上述3种转换的递归实现 注:进制的表示: 二进制:开头是0b,eg: 0b1011(注:c/ ...

  7. 解决nginx在记录post数据时 中文字符转成16进制的问题【转载】

    1. 问题描述 nginx 在获取post数据时候,如果是中文,则转换成16进制显示在日志文件中,如下图所示.   Paste_Image.png 日志格式为: log_format postdata ...

  8. Oracle中如何进行进制转换(2进制,10进制,16进制)

    1.16进制转换为10进制 可以通过to_number函数实现 SQL> select to_number('19f','xxx') from dual; TO_NUMBER('19F','XX ...

  9. 轻松实现C/C++各种常见进制相互转换

    其它进制转为十进制 在实现这个需求之前,先简单介绍一个c标准库中的一个函数: long strtol( const char *str, char **str_end, int base); 参数详细 ...

  10. 洛谷 题解 P1604 【B进制星球】

    题目:P1604 B进制星球 本人提交记录:R6292872 作为一个极其无聊的人,我没事干地写了operator... 思路很简单: 读入b 读入b进制的x,y ans = x + y 输出ans ...

随机推荐

  1. CCNA第一讲笔记

    园区网:一组连续的局域网(校园网.企业内部网) 园区网拓扑: 一层楼的PC连接到一台交换机(同一层的PC可以互联):一栋楼的每层的交换机连接到同一台交换机(整栋楼的PC可以互联):每栋楼的交换机连接到 ...

  2. 用端口映射在Apache中对每个项目分配端口,实现一个端口访问一个网站

    映口映射的功能主要就是实际互联网可以访问当前进行映射的电脑,首先我们要进行路由器的端口映射,常见的开放端口有80,21,3389等,80是网站常用端口,21是FTP服务器常用端口,3389是远程桌面连 ...

  3. HttpClient使用笔记

    使用版本为4.5.1 常用API: 1.获取网页内容:InputStream in = response.getEntity().getContent() 2.获取状态码:response.getSt ...

  4. WPF-实现Windows 7 样式 Aero TreeView 控件

    关于实现Windows 7 样式 Aero TreeView 控件的具体过程可以参考如下文章 http://www.cnblogs.com/o2ds/archive/2010/06/22/176302 ...

  5. oracle中的隐式提交(auto commit)

    通常我们执行sql或pl/sql时,需要我们手工提交.这样才能使所做的更改永久保存到数据库. 但有时即使我们没有在sql或pl/sql中发出commit命令,所做的更改也会被提交.这种提交是在某些特定 ...

  6. 【css】web标准

    网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior) 结构重点理解: XHTML 1.应用形式 ccs+div  2000 2.基于xml 和x ...

  7. 【WS-Federation】到底有多少公司在用WS-Federation

    到底有多少公司在用WS-Federation? sso先调用一个登录接口 获取一个token 然后再调用各种业务接口 如果是ssl token 直接暴露就行了 没有ssl 最好每次取一个token, ...

  8. 泛形集合List<T>

    public class Person { /// <summary> /// 姓名 /// </summary> private string name; public st ...

  9. Insist

    1.怎么自动截断文本? 如题,当数据库中的数据内容超出了要显示的长度时,如果不采取措施,会破坏页面的布局美观,所以可以采用自动截断文本,需要查看的时候再把其他的内容显示出来. 没截断的时候如下图: 再 ...

  10. shell 后台执行命令

    shell 后台执行命令方法: 1. nohup cmd &          后台会生成 nohup.out 文件 2.cmd >/路径/xx.log &   后台生成 xx. ...