atoi、atof、itoa、itow函数简介

2024-02-20 19:58
文章标签 函数 简介 itoa atoi atof itow

本文主要是介绍atoi、atof、itoa、itow函数简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



本文转自:http://blog.sina.com.cn/s/blog_53a732bb0100ws2v.html

atoi、atof、itoa、itow函数是windows平台下实现字符串与数值相互转换的函数。Linux平台下请使用标准库中的sprintf与sscanf函数。

 
atoi函数

原型:int atoi( const char *string );

ASCII to integer

作用:将字符串转为integer类型

 

atof函数

原型:double atof( const char *string );

ASCII to float

作用:将字符串转为double类型

 

对于以上函数,若字符串无法转化为合法的数值类型,函数将返回0 。

使用范例(来自MSDN):
atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用Code
 1atoi、atof、_itoa、_itow 函数使用#include <stdlib.h>
 2atoi、atof、_itoa、_itow 函数使用#include <stdio.h>
 3atoi、atof、_itoa、_itow 函数使用
 4atoi、atof、_itoa、_itow 函数使用void main( void )
 5atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用{
 6atoi、atof、_itoa、_itow 函数使用   char *s; double x; int i; long l;
 7atoi、atof、_itoa、_itow 函数使用
 8atoi、atof、_itoa、_itow 函数使用   printf( " testing atoi,atof,atol function :\n" ;
 9atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   = "  -2309.12E-15"   
10atoi、atof、_itoa、_itow 函数使用   = atof( );
11atoi、atof、_itoa、_itow 函数使用   printf( "atof test: ASCII string: %s\tfloat:  %e\n"s, );
12atoi、atof、_itoa、_itow 函数使用
13atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   = "7.8912654773d210" 
14atoi、atof、_itoa、_itow 函数使用   = atof( );
15atoi、atof、_itoa、_itow 函数使用   printf( "atof test: ASCII string: %s\tfloat:  %e\n"s, );
16atoi、atof、_itoa、_itow 函数使用
17atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   = "  -9885 pigs"     
18atoi、atof、_itoa、_itow 函数使用   = atoi( );
19atoi、atof、_itoa、_itow 函数使用   printf( "atoi test: ASCII string: %s\t\tinteger: %d\n"s, );
20atoi、atof、_itoa、_itow 函数使用
21atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   = "98854 dollars"    
22atoi、atof、_itoa、_itow 函数使用   = atol( );
23atoi、atof、_itoa、_itow 函数使用   printf( "atol test: ASCII string: %s\t\tlong: %ld\n"s, );
24atoi、atof、_itoa、_itow 函数使用}
25atoi、atof、_itoa、_itow 函数使用
 

输出:

atof test: ASCII string:   -2309.12E-15 float: -2.309120e-012

atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210

atoi test: ASCII string:   -9885 pigs    integer: -9885

atol test: ASCII string: 98854 dollars    long: 98854


 

_itoa函数

原型:char *_itoa( int value, char *str, int radix  );//2<=radix<=36

Integer to ASCII

作用:将Integer类型转换为radix进制,然后以ASCII字符串的形式存放在str中

 

_itow函数

wchar_t * _itow( int value, wchar_t *str, int radix ); //2<=radix<=36

Integer to Wide Char

作用:将Integer类型转换为radix进制,然后以宽字符串的形式存放在str中
 

    以上2个函数均有安全隐患(当字符数组长度不足保存结果时会导致缓冲区溢出),在vs2008中编译时会有警告。推荐使用它们的安全版本—— _itoa_s与_itow_s 。

 
_itoa_s 函数原型如下:

 errno_t _itoa_s(

   int value,

   char *buffer,

   size_t sizeInCharacters,  //存放结果的字符数组长度

   int radix

);

当转换的结果长度比sizeInCharacters变量大时,由于出现access violation,函数将马上终止,而_itoa函数将继续运行。

使用范例(来自MSDN):

atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用Code
 1atoi、atof、_itoa、_itow 函数使用#include<string.h>
 2atoi、atof、_itoa、_itow 函数使用#include<stdlib.h>
 3atoi、atof、_itoa、_itow 函数使用#include<stdio.h>
 4atoi、atof、_itoa、_itow 函数使用
 5atoi、atof、_itoa、_itow 函数使用int main( void )
 6atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用{
 7atoi、atof、_itoa、_itow 函数使用   char buffer[65];
 8atoi、atof、_itoa、_itow 函数使用   int r;
 9atoi、atof、_itoa、_itow 函数使用   printf( "test _itoa function \n" ;
10atoi、atof、_itoa、_itow 函数使用   forr=10r>=2--)
11atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   atoi、atof、_itoa、_itow 函数使用{
12atoi、atof、_itoa、_itow 函数使用     _itoa( -1buffer, ); // C4996
13atoi、atof、_itoa、_itow 函数使用     // Note: _itoa is deprecated; consider using _itoa_s instead
14atoi、atof、_itoa、_itow 函数使用     printf( "base %d: %s (%d chars)\n"r, buffer, strnlen(buffer, _countof(buffer)) );
15atoi、atof、_itoa、_itow 函数使用   }
16atoi、atof、_itoa、_itow 函数使用
17atoi、atof、_itoa、_itow 函数使用   printf( "test _itoa_s function \n " ;
18atoi、atof、_itoa、_itow 函数使用   forr=10r>=2--)
19atoi、atof、_itoa、_itow 函数使用atoi、atof、_itoa、_itow 函数使用   atoi、atof、_itoa、_itow 函数使用{
20atoi、atof、_itoa、_itow 函数使用      _itoa_s( -1buffer, 65);
21atoi、atof、_itoa、_itow 函数使用      printf( "base %d: %s (%d chars)\n"r, buffer, strnlen(buffer, _countof(buffer)) );
22atoi、atof、_itoa、_itow 函数使用   }
23atoi、atof、_itoa、_itow 函数使用}
24atoi、atof、_itoa、_itow 函数使用

 

输出:

base 10: -1 (2 chars)

base 9: 12068657453 (11 chars)

base 8: 37777777777 (11 chars)

base 7: 211301422353 (12 chars)

base 6: 1550104015503 (13 chars)

base 5: 32244002423140 (14 chars)

base 4: 3333333333333333 (16 chars)

base 3: 102002022201221111210 (21 chars)

base 2: 11111111111111111111111111111111 (32 chars)

 

base 10: -1 (2 chars)

base 9: 12068657453 (11 chars)

base 8: 37777777777 (11 chars)

base 7: 211301422353 (12 chars)

base 6: 1550104015503 (13 chars)

base 5: 32244002423140 (14 chars)

base 4: 3333333333333333 (16 chars)

base 3: 102002022201221111210 (21 chars)

base 2: 11111111111111111111111111111111 (32 chars)

这篇关于atoi、atof、itoa、itow函数简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/729373

相关文章

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以