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

相关文章

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

Python lambda函数(匿名函数)、参数类型与递归全解析

《Pythonlambda函数(匿名函数)、参数类型与递归全解析》本文详解Python中lambda匿名函数、灵活参数类型和递归函数三大进阶特性,分别介绍其定义、应用场景及注意事项,助力编写简洁高效... 目录一、lambda 匿名函数:简洁的单行函数1. lambda 的定义与基本用法2. lambda

Python 函数详解:从基础语法到高级使用技巧

《Python函数详解:从基础语法到高级使用技巧》本文基于实例代码,全面讲解Python函数的定义、参数传递、变量作用域及类型标注等知识点,帮助初学者快速掌握函数的使用技巧,感兴趣的朋友跟随小编一起... 目录一、函数的基本概念与作用二、函数的定义与调用1. 无参函数2. 带参函数3. 带返回值的函数4.

MySQL中DATE_FORMAT时间函数的使用小结

《MySQL中DATE_FORMAT时间函数的使用小结》本文主要介绍了MySQL中DATE_FORMAT时间函数的使用小结,用于格式化日期/时间字段,可提取年月、统计月份数据、精确到天,对大家的学习或... 目录前言DATE_FORMAT时间函数总结前言mysql可以使用DATE_FORMAT获取日期字段

MySQL 索引简介及常见的索引类型有哪些

《MySQL索引简介及常见的索引类型有哪些》MySQL索引是加速数据检索的特殊结构,用于存储列值与位置信息,常见的索引类型包括:主键索引、唯一索引、普通索引、复合索引、全文索引和空间索引等,本文介绍... 目录什么是 mysql 的索引?常见的索引类型有哪些?总结性回答详细解释1. MySQL 索引的概念2

Django中的函数视图和类视图以及路由的定义方式

《Django中的函数视图和类视图以及路由的定义方式》Django视图分函数视图和类视图,前者用函数处理请求,后者继承View类定义方法,路由使用path()、re_path()或url(),通过in... 目录函数视图类视图路由总路由函数视图的路由类视图定义路由总结Django允许接收的请求方法http

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数