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. Android 源码编译 步骤

    添加资源后编译步骤 1:lunch 112:mmm frameworks/base/core/res/生成Install: out/target/product/hammerhead/system/f ...

  2. 使用WebJar管理css、JavaScript文件

    Web前端使用了越来越多的JS或CSS,如jQuery, Backbone.js 和Bootstrap.一般情况下,我们是将这些Web资源拷贝到Java的目录下,通过手工进行管理,这种通方式容易导致文 ...

  3. Dao层和Service层设计

    1.Dao接口层 public interface IBaseDao<T, ID extends Serializable>{ public abstract Serializable s ...

  4. CentOs7&zookeeper

    下载安装包 wget http://www.apache.dataguru.cn/zookeeper/stable/zookeeper-3.4.6.tar.gz 解压 tar xzvf zookeep ...

  5. javascript 面向对象技术

    面向对象术语 对象 ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 尽管 ECMAScript 如 ...

  6. 利用javascript调用摄像头,可以配合socket开发监控系统

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  7. NodeJS + Socket.io搭建聊天服务器

    第一步:安装node git clone https://github.com/joyent/node.git cd node git checkout v0.10.33-release ./conf ...

  8. JS中如何判断null

    var exp = null; if (exp == null) { alert("is null"); } exp 为 undefined 时,也会得到与 null 相同的结果, ...

  9. 几个好用的截取字符串的php函数分享

    分享几个好用的PHP 截取字符串函数(支持gb2312和utf-8). 1.截取GB2312字符用的函数 <?php /** **截取中文字符串 * edit by www.jbxue.com ...

  10. xml学习总结(四)

    命名空间 (1)产生 问题:在不同的约束文档中,有不同好安逸的相同标记名称 解决办法 每个约束模式人当被赋予一个唯一的名称空间,每个名称空间可用一个唯一的URI表示 在XML实例中为来自不同模式文档的 ...