itoa : Convert integer to string
itoa
char * itoa ( int value, char * str, int base );
Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.
If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, valueis always considered unsigned.
str should be an array long enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.
Parameters
- value
- Value to be converted to a string.
- str
- Array in memory where to store the resulting null-terminated string.
- base
- Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
Return Value
A pointer to the resulting null-terminated string, same as parameter str.
Portability
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
A standard-compliant alternative for some cases may be sprintf:
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base.
Example
/* itoa example */
#include <stdio.h>
#include <stdlib.h> int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}
Output:
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110
itoa : Convert integer to string的更多相关文章
- Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?
Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...
- How to: Convert Between Various String Types
This topic demonstrates how to convert various Visual C++ string types into other strings. The str ...
- Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...
- java 13-4 Integer和String、int之间的转换,进制转换
1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...
- Integer.valueOf(String) 方法之惑
本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...
- [转]Integer.valueOf(String) 方法之惑
具体问题以前偶然遇到过,好象是一个java答题得分的论坛,当时做错还研究了下怎么回事,但是前两天遇到类似问题却没想起来.巩固下基础,转了下面文章. 以下内容转自:http://www.importne ...
- Integer.parseInt(String s, int radix)方法介绍(修正版)
先来说明一下Integer.parseInt(String s, int radix)的功能. Integer.parseInt(String s, int radix)就是将整数字符串s(radix ...
- Object转Integer,String
object先转为字符串,然后通过int 的封装类(Integer)的pasreInt()方法转为int 例如: Object ob = 123; Integer.parseInt(String.v ...
- ToString()、Convert.ToString()、(string)、as string 的区别
通常 object 到 string 有四种方式(假设有object obj):obj.ToString().Convert.ToString().(string)obj.obj as string. ...
随机推荐
- POJ2299: Ultra-QuickSort-合并排序解决逆序数问题
#include<iostream> #include<malloc.h> using namespace std; long long ans; void merge(int ...
- Select the best path in a matrix
Amazon interview question: Given a 2-dimensional array with arbitrary sizes and contains random posi ...
- 关于arm处理器 内存编址模式 与 字节对齐方式 (转)
转自:http://bavon.bokee.com/5429805.html 在x86+Linux上写的程序,在PC机上运行得很好.可是使用ARM的gcc进行交叉编译,再送到DaVinci目标板上运行 ...
- eclipse老是卡在Refreshing workspace
最近老是遇到eclipse没有响应,参考网络上的相关帖子做了些优化,但问题依旧. 后来发现出现这个问题一般都是eclipse无响应,直接结束进程后导致的,看了下eclipse日志,果真如此. !SES ...
- css3水平翻转
@keyframes cardFront { 0%, 40%, 100% { 02 opacity:1; 03 -webkit-transform:rotateY(0deg); 04 ...
- Spring Framework 5.0.0.M3中文文档 翻译记录 introduction
翻译自: http://docs.spring.io/spring/docs/5.0.0.M3/spring-framework-reference/htmlsingle/#spring.tld.ha ...
- EasyUI Datagrid 自定义列、Foolter及单元格编辑
1:自定义列,包括 Group var head1Array = []; head1Array.push({ field: 'Id', title: 'xxxx', rowspan: 2 }); he ...
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
地化GO的时候一直遇到一个错误就是check the manual that corresponds to your MySQL server version for the right syntax ...
- DirectoryEntry 活动目录的使用
public class DirectoryHelper { public static string DomainName = System.Environment.UserDomainName; ...
- C++ Primer--虚函数与纯虚函数的区别
首先:强调一个概念 定义一个函数为虚函数,不代表函数为不被实现的函数. 定义他为虚函数是为了允许用基类的指针来调用子类的这个函数. 定义一个函数为纯虚函数,才代表函数没有被实现. 定义纯虚函数是为了实 ...