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

相关文章

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C