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 ...
随机推荐
- Redirect and POST in ASP.NET
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
- TweenMax动画库学习(六)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- jQuery网页元素拖拽插件
效果说明:配合已有CSS样式,载入插件后,网页元素可以随意在窗口内拖拽,设置了原位置半透明和拖拽半透明的效果选项,可根据需要选择.另外,当页面上有多个可拖拽元素时,可以载入另外一个用于设置z-inde ...
- asp:HyperLink vs asp:LinkButton
asp:HyperLink NavigateUrl - 页面刷新为新的页面 pageload ispostback = false asp:LinkButton PostbackUrl - postb ...
- ComboBox Control Messages 消息
连接到MSDN,有时间完善这个.具体说明可点击进入msdn CB_ADDSTRING 添加一个字符串组合框的列表框.如果组合框没有cbs_sort风格,字符串添加到列表的结尾.否则,该字符串插入列表, ...
- IOS系统框架
IOS系统框架:ios架构号MAC OS的基础框架相似:ios扮演底层硬件和应用程序的中介,底层为所有应用程序提供基础服务,高层则包含一些复杂巧妙的技术和服务,并且创建的应用程序不能直接访问硬件. C ...
- ASP.NET 页面传值得9种方式
1. Get(即使用QueryString显式传递) 方式:在url后面跟参数. 特点:简单.方便. 缺点:字符串长度最长为255个字符:数据泄漏在url中. 适用数据 ...
- Unity3d 协程、调用函数、委托
(一)协程 开启方法:StartCoroutine("函数名"): 结束方法StopCoroutine("函数名"),StopAllCoroutines(); ...
- 进入 App Store 打分
很多用户用了好软件后忘记或嫌麻烦而不去 App Store 进行打分评星,为此开发者可以在应用中加入打分按钮,点击后直接跳转到 App Store 的评分界面. App Store 上评论的链接地址是 ...
- ios9新特性概述
1.iPad的分屏功能很重要. 开发者对iPad的分屏功能感到兴奋,并认为其对苹果未来非常重要.电子邮件信息应用Hop创始人艾瑞兹·皮洛索夫(Erez Pilosof)认为,如果苹果如传闻中那样决定推 ...