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 ...
随机推荐
- 在MAC OS X下安装usb转串口驱动(PL2303主控芯片)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 因为最近手里有一块STM32Discovery开发板,所以想搞一下STM32的开发,我前面的 ...
- HTML5新增结构标签
引言 在本节中,笔者将向大家讲述三部分内容,分别介绍HTML5时代的召唤,跟HTML4的区别,以及HTML5中带来的新的结构标签. HTML5时代的召唤 HTML4与HTML5的区别 HTML5新结构 ...
- javascript 事件的学习
1.事件绑定: addEventListener , removeEventListener.是dom2级别的事件绑定 attachEvent , detachEvent 是IE的事件绑定. 2. 事 ...
- Spark小课堂Week3 FirstSparkApp(Dataframe开发)
Spark小课堂Week3 FirstSparkApp(代码优化) RDD代码简化 对于昨天练习的代码,我们可以从几个方面来简化: 使用fluent风格写法,可以减少对于中间变量的定义. 使用lamb ...
- Perl中的正则表达
前几天用到了Perl语言,主要看了一下Perl中的正则表达式,在各种网页语言中,正则表达式在处理字符串的时候十分有用,所以这里就简单说一下在Perl中正则表达式的应用. 先上代码 #!/usr/bin ...
- uva401 - Palindromes结题报告
题目地址 : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 生成XML文件,通过实体生成XML文件
实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- Linked List Sorting (链表)
Linked List Sorting (链表) A linked list consists of a series of structures, which are not necessari ...
- P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
裸的强连通 ; type node=record f,t:longint; end; var n,m,dgr,i,u,v,num,ans:longint; bfsdgr,low,head,f:arra ...