itoa()、atoi()、任意进制转换
itoa --功能:将任意类型的数字转换为字符串。在<stdlib.h>中与之有相反功能的函数是atoi。
atoi----功 能: 将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
代码1:itoa 实现任意进制的转换(整形-->字符串)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
int number=15;
char string[25];
itoa(number,string,4);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,2);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,8);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,10);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,16);
printf("integer=%d string=%s\n",number,string);
return 0;
}
<img src="http://img.blog.csdn.net/20150407213001960?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzUzMzI4OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
int number=16;
char string[25];
float n;
char str[]="12345.67";
n=atoi(str);
printf("string=%s float=%f\n",str,n);
return 0;
}
<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
char a[]="-100";
char b[]="123";
int c;
c=atoi(a)+atoi(b);
printf("c=%d\n",c);
return 0;
}
记住一点:itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似。
代码3:虽然可能itoa无法使用,但是我们可以编写自己的itoa()函数,以下是实现源代码(来源网络):
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span><pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
using namespace std; char*my_itoa(int num,char*str,int radix){//原数字,存放地址,要转换的转换进制
const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
char*ptr=str ;
bool negative=false ;
if(num==0){
//num=0
*ptr++='0' ;
*ptr='/0' ;
// don`t forget the end of the string is '/0'!!!!!!!!!
return str ;
}
if(num<0){
//if num is negative ,the add '-'and change num to positive
*ptr++='-' ;
num*=-1 ;
negative=true ;
}
while(num){
*ptr++=table[num%radix];
num/=radix ;
}
*ptr='\0' ;
//if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char*start=(negative?str+1:str);
//now start points the head of the string
ptr--;
//now prt points the end of the string
while(start<ptr){
char temp=*start ;
*start=*ptr ;
*ptr=temp ;
start++;
ptr--;
}
return str ;
}
int main(){
int a=15;
char str[100];
my_itoa(a,str,8);
printf("%s\n",str);
return 0;
}
代码4:任意进制间的转换 (在任意进制之间进行转换,通过十进制中介。)
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span></span><pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std; long toTen(char a[],int bit){//任意进制到10进制。(a是要转换的数,bit是原本的进制(2~36)----要用数组存储要转换的数字,结果返回整型的十进制数)
long i,b=1,sum=0;
int length=strlen(a);
for (i=length-1;i>=0;i--){
if (a[i]>='A'){
sum+=(a[i]-'A'+10)*b;
b*=bit;
}
else{
sum+=(a[i]-'0')*b;
b*=bit;
}
}
return sum;
}
int main(){
int aNum;
char bNum[20];
//以整型读入,转换字符串带入函数,进行进制转换
cin>>aNum;
sprintf(bNum,"%d",aNum);
cout<<toTen(bNum,8)<<endl; //假设原本是8进制,代入函数后返回10进制的数 //以字符串读入,直接代入函数,进行进制转换
cin>>bNum;
cout<<toTen(bNum,2)<<endl; //假设原本是2进制 //把二进制10110转换为十六进制
aNum=toTen("1111",2);
itoa(aNum,bNum,16);
cout<<bNum<<endl;
return 0;
}

itoa()、atoi()、任意进制转换的更多相关文章
- poj1220 (高精度任意进制转换)
http://poj.org/problem?id=1220 高精度任意进制转换 代码是从discuss里找到的,据说是maigo神牛写的. 超精简!! 我自己第一写的时候,还把n进制先转成10进制, ...
- python任意进制转换
python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...
- Python版任意进制转换
def decimalToAny(num,n): baseStr = {10:"a",11:"b",12:"c",13:"d&qu ...
- 2~62位任意进制转换(c++)
进制转换的符号表为[0-9a-zA-Z],共61个字符,最大可表示62进制. 思路是原进制先转换为10进制,再转换到目标进制. 疑问: 对于负数,有小伙伴说可以直接将符号丢弃,按照整数进行进位转换,最 ...
- 【C/C++】任意进制转换
进制转换:R进制->10进制:10进制->R进制. #include<bits/stdc++.h> using namespace std; /*函数:r进制转换成10进制*/ ...
- (任意进制转换)将 r 进制数转成 k 进制数
我们知道任意进制转换为十进制,都是乘以基数的多少次方,然后相加: 十进制转换为任意进制,都是除以基数,然后倒着取余数: 所以这里是用十进制数中转,实现任意进制数的转换 #include<iost ...
- lua之m进制转换为n进制-任意进制转换算法
够无聊的写这个,为防止需要的人也无聊一遍,写个吧 算法有n种,但是,咱们一种就够用了 --数组倒序排列 local function orderByDesc( input ) local output ...
- poj1220(短除法实现任意进制转换)
题目链接:https://vjudge.net/problem/POJ-1220 题意:给定a进制的大数s,将其转换为b进制.其中2<=a,b<=62. 题意:一般进制转换是以10进制为中 ...
- 在线任意进制转换工具 - aTool在线工具
http://www.atool.org/hexconvert.php ss = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ ...
随机推荐
- 小程序学习笔记二:页面文件详解之 .json文件
页面配置文件—— pageName.json 每一个小程序页面可以使用.json文件来对本页面的窗口表现进行配置,页面中配置项会覆盖 app.json 的 window 中相同的配置项. 页面的 ...
- MATLAB 程序处理结果出现 NAN 问题
1)0/0 或者说 任意常数/0 也就是0不能做分母. (nan出现的情况绝大部分是分母出现0了) 若分子为0的情况,(分母不为0),结果也应该是0而非 NAN. 2)如果是 无穷大比无穷大 ...
- TensorFlow精选Github开源项目
转载于:http://www.matools.com/blog/1801988 TensorFlow源码 https://github.com/tensorflow/tensorflow 基于Tens ...
- ionic3 表单输入元素input的三种事件
1.onblur事件 onblur事件即为失去焦点时触发的事件,而所谓的焦点就是一闪一闪的光标.而ionic中onblur则为ionBlur,具体用法如下: //html<ion-input [ ...
- app优化之流量节省
前言:“客户端上传时间戳”的玩法,你玩过么?一起聊聊时间戳的奇技淫巧!,其实这个类似于数据版本号的东西. 缘起:无线时代,流量敏感.APP在登录后,往往要向服务器同步非常多的数据,很费流量,技术上有没 ...
- [druid]大数据挑战——如何使用Druid实现数据聚合
-- 知道你为什么惧组件很多的一些开源软件? 因为缺乏阅读能力. 最近我接手了druid+kafka+elk一套等日志系统. 但是我对druid很陌生, 周旋了几天, 官网文档快速开始照着做了下. 看 ...
- (7) MySQL数据库备份详解
对于任何数据库来说,备份都是非常重要的 数据库复制不能取代备份的作用 比如我们由于误操作,在主数据库上删除了一些数据,由于主从复制的时间很短,在发现时,从数据库上的数据可能也已经被删除了, 我们不能使 ...
- [MySQL Reference Manual]17 Group Replication
17 Group Replication 17 Group Replication 17.1 Group Replication后台 17.1.1 Replication技术 17.1.1.1 主从复 ...
- Machine Learning第十一周笔记:photo OCR
博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) 刚刚完毕了Cousera上Machine Learning的最后一周课程.这周介绍了 ...
- find ctime 加减n时间范围
看下atime的时间解释:-atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour ...