MFC數據類型轉換 _itoa atoi、atof、itoa、itow _itoa_s


_itoa

功能:把一整數轉換為字符串

用法:char * _itoa(int value, char *string, int radix);

  詳細解釋: _itoa是英文integer to array(將int整型數轉化為一個字符串,並將值保存在數組string中)的縮寫.其中value為要轉化的整數, radix是基數的意思,即先將value轉化為radix進制的數,之后再保存在string中.

  備注:該函數的頭文件是"stdlib.h"

  程序例:

  #include <stdlib.h>

  #include <stdio.h>

  int main()

  {

  int number = 123456;

  char string[25];

  _itoa(number, string, 10);

  printf("integer = %d string = %s\n", number, string);

  return 0;

  }

  注釋:編譯系統:VC++6.0,TC不支持。


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/bufrfish/archive/2008/11/03/3209167.aspx

 

atoi、atof、_itoa、_itow 函數使用

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):
復制代碼

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
復制代碼

輸出:

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):

Code

 

輸出:

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM