国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
字符串轉(zhuǎn)換為數(shù)值的庫(kù)函數(shù)
atoi、atof、_itoa、_itow 函數(shù)使用

atoi、atof、itoa、itow函數(shù)是windows平臺(tái)下實(shí)現(xiàn)字符串與數(shù)值相互轉(zhuǎn)換的函數(shù)。Linux平臺(tái)下請(qǐng)使用標(biāo)準(zhǔn)庫(kù)中的sprintf與sscanf函數(shù)。


atoi函數(shù)

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

ASCII to integer

作用:將字符串轉(zhuǎn)為integer類型

 

atof函數(shù)

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

ASCII to float

作用:將字符串轉(zhuǎn)為double類型

 

對(duì)于以上函數(shù),若字符串無(wú)法轉(zhuǎn)化為合法的數(shù)值類型,函數(shù)將返回0 。

使用范例(來(lái)自MSDN):

1
#include <stdlib.h>
2
#include
<stdio.h>
3

4
void main( void )
5
{
6
   
char *s; double x; int i; long l;
7

8
    printf(
" testing atoi,atof,atol function :\n" ) ;
9
    s
= "   -2309.12E-15";    /**//* Test of atof */
10
    x
= atof( s );
11
    printf(
"atof test: ASCII string: %s\tfloat:   %e\n", s, x );
12

13
    s
= "7.8912654773d210";  /**//* Test of atof */
14
    x
= atof( s );
15
    printf(
"atof test: ASCII string: %s\tfloat:   %e\n", s, x );
16

17
    s
= "   -9885 pigs";      /**//* Test of atoi */
18
    i
= atoi( s );
19
    printf(
"atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
20

21
    s
= "98854 dollars";     /**//* Test of atol */
22
    l
= atol( s );
23
    printf(
"atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
24
}

25
輸出:
testing atoi,atof,atol function:

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函數(shù)

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

Integer to ASCII

作用:將Integer類型轉(zhuǎn)換為radix進(jìn)制,然后以ASCII字符串的形式存放在str中

 

_itow函數(shù)

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

Integer to Wide Char

作用:將Integer類型轉(zhuǎn)換為radix進(jìn)制,然后以寬字符串的形式存放在str中

    以上2個(gè)函數(shù)均有安全隱患(當(dāng)字符數(shù)組長(zhǎng)度不足保存結(jié)果時(shí)會(huì)導(dǎo)致緩沖區(qū)溢出),在vs2008中編譯時(shí)會(huì)有警告。推薦使用它們的安全版本—— _itoa_s與_itow_s 。


_itoa_s 函數(shù)原型如下:

errno_t _itoa_s(

   int value,

   char *buffer,

   size_t sizeInCharacters, //存放結(jié)果的字符數(shù)組長(zhǎng)度

   int radix

);

當(dāng)轉(zhuǎn)換的結(jié)果長(zhǎng)度比sizeInCharacters變量大時(shí),由于出現(xiàn)access violation,函數(shù)將馬上終止,而_itoa函數(shù)將繼續(xù)運(yùn)行。

使用范例(來(lái)自MSDN):

Code
1
#include<string.h>
2
#include
<stdlib.h>
3
#include
<stdio.h>
4

5
int main( void )
6
{
7
   
char buffer[65];
8
   
int r;
9
    printf(
"test _itoa function ! \n" ) ;
10
   
for( r=10; r>=2; --r )
11
   
{
12
      _itoa(
-1, buffer, r ); // C4996
13
     
// Note: _itoa is deprecated; consider using _itoa_s instead
14
      printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
15
    }

16

17
    printf(
"test _itoa_s function ! \n " ) ;
18
   
for( r=10; r>=2; --r )
19
   
{
20
       _itoa_s(
-1, buffer, 65, r );
21
       printf(
"base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
22
    }

23
}

24

 

輸出:

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)

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
CString類常用方法----Format(),sprintf(),itoa(),ltoa(),ultoa(),atoi(),atol(),atof()
數(shù)字和字符串之間轉(zhuǎn)換相關(guān)的函數(shù)
C/C 字符串和數(shù)字互換方案收集(轉(zhuǎn))
C語(yǔ)言的常用類型轉(zhuǎn)換函數(shù)(atoi,atol,strtod,strtol,strtoul)...
C語(yǔ)言itoa()函數(shù)和atoi()函數(shù)詳解(整數(shù)轉(zhuǎn)字符C實(shí)現(xiàn))
字符串轉(zhuǎn)換整數(shù)及整數(shù)轉(zhuǎn)換字符串
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服