头文件:<stdlib.h>

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



atoi----功 能: 将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
用 法: int atoi(const char *nptr);





代码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()、任意进制转换的更多相关文章

  1. poj1220 (高精度任意进制转换)

    http://poj.org/problem?id=1220 高精度任意进制转换 代码是从discuss里找到的,据说是maigo神牛写的. 超精简!! 我自己第一写的时候,还把n进制先转成10进制, ...

  2. python任意进制转换

    python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...

  3. Python版任意进制转换

    def decimalToAny(num,n): baseStr = {10:"a",11:"b",12:"c",13:"d&qu ...

  4. 2~62位任意进制转换(c++)

    进制转换的符号表为[0-9a-zA-Z],共61个字符,最大可表示62进制. 思路是原进制先转换为10进制,再转换到目标进制. 疑问: 对于负数,有小伙伴说可以直接将符号丢弃,按照整数进行进位转换,最 ...

  5. 【C/C++】任意进制转换

    进制转换:R进制->10进制:10进制->R进制. #include<bits/stdc++.h> using namespace std; /*函数:r进制转换成10进制*/ ...

  6. (任意进制转换)将 r 进制数转成 k 进制数

    我们知道任意进制转换为十进制,都是乘以基数的多少次方,然后相加: 十进制转换为任意进制,都是除以基数,然后倒着取余数: 所以这里是用十进制数中转,实现任意进制数的转换 #include<iost ...

  7. lua之m进制转换为n进制-任意进制转换算法

    够无聊的写这个,为防止需要的人也无聊一遍,写个吧 算法有n种,但是,咱们一种就够用了 --数组倒序排列 local function orderByDesc( input ) local output ...

  8. poj1220(短除法实现任意进制转换)

    题目链接:https://vjudge.net/problem/POJ-1220 题意:给定a进制的大数s,将其转换为b进制.其中2<=a,b<=62. 题意:一般进制转换是以10进制为中 ...

  9. 在线任意进制转换工具 - aTool在线工具

    http://www.atool.org/hexconvert.php ss = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ ...

随机推荐

  1. SpringBoot2.x使用EasyPOI导入Excel浅谈

    SpringBoot2.x使用EasyPOI导入Excel浅谈 平时经常遇到客户要帮忙导入一些数据到数据库中,有些数据比较多有时候手动录入就会很耗时间,所以就自己写一个Excel导入的demo记录一下 ...

  2. [Oracle] “表中有数据,但select count(*)的结果为0”问题的解决办法

    一.问题 今天遇到了一个神奇的问题--表中有数据,但select count(*)的结果为0. 这个问题最初的表现形式是"查询报表没有分页". 最开始还以为是java端的问题.后来 ...

  3. dagger2 重点笔记

    官方架构例子,里面有个dagger2的结合的例子 https://github.com/googlesamples/android-architecture https://google.github ...

  4. mac 使用笔记日志

    telnet安装 安装homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...

  5. “5W1H”带你来学习JavaScript

    上次的设计模式讲课,从中学习到了非常多.不仅是技术上,更重要的是怎样来学习.我们学习的技术.科技的更新速度超过我们的想象,对于我们这个有生命年限的个体,怎样可以在有生之年可以让自己立足于科技的不败浪潮 ...

  6. C#获取中国天气网免费天气预报信息

    中国天气网接口地址:”http://wthrcdn.etouch.cn/WeatherApi?citykey=” + weatherCityCode(为城市code); 下面是转化过程中我们需要用到的 ...

  7. Centos7.4别名设置提高工作效率

    一.打开 .bashrc文件 1.位置:~(cd ~)目录下 2.cat .bashrc 原文件内容如下: # .bashrc # User specific aliases and function ...

  8. atitit r9 doc on home ntpc .docx

    卷 p2soft 的文件夹 PATH 列表 卷序列号为 9AD0-D3C8 D:. │  Aittit pato 面对拒绝  的回应.docx │  Atitit  中国明星数量统计 attilax. ...

  9. textarea 分割

    var orderNo = $("#orderNo").val();var orderNo = orderNo.toString().split(/\r?\n/);

  10. DirectX using C++_error X3539:ps1_x is no longer supported...解决方案

    问题来源 在研究HLSL时编译一个demo出现了error X3539的问题 解决方案 将代码中的ps_1_1 改为ps_2_0 PixelShader = compile ps_1_1 PS(); ...