如何转换字符串为数值- -
2006-04-29 17:33
513 查看
MFC中的CString和STL中的string似乎并没有提供相关的函数将字符串转换为数值。
以下是一些可能用到的函数:
atoi atol atof
strtol strtoul strtod
以下代码演示了前三个函数的用法:
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
}
以下代码演示了后三个函数的用法:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char *string, *stopstring;
double x;
long l;
int base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s/n", string );
printf(" strtod = %f/n", x );
printf(" Stopped scan at: %s/n/n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s", string );
printf(" strtol = %ld", l );
printf(" Stopped scan at: %s", stopstring );
string = "10110134932";
printf( "string = %s/n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( " strtol = %ld (base %d)/n", ul, base );
printf( " Stopped scan at: %s/n", stopstring );
}
return 0;
}
更详细的数据转换函数如下:
abs Find absolute value of integer
atof Convert string to float
atoi, _atoi64 Convert string to int
atol Convert string to long
_ecvt Convert double to string of specified length
_fcvt Convert double to string with specified number of digits following decimal point
_gcvt Convert double number to string; store string in buffer
_itoa, _i64toa, _itow, _i64tow Convert int to string
labs Find absolute value of long integer
_ltoa, _ltow Convert long to string
_mbbtombc Convert 1-byte multibyte character to corresponding 2-byte multibyte character
_mbcjistojms Convert Japan Industry Standard (JIS) character to Japan Microsoft (JMS) character
_mbcjmstojis Convert JMS character to JIS character
_mbctohira Convert multibyte character to 1-byte hiragana code
_mbctokata Convert multibyte character to 1-byte katakana code
_mbctombb Convert 2-byte multibyte character to corresponding 1-byte multibyte character
mbstowcs Convert sequence of multibyte characters to corresponding sequence of wide characters
mbtowc Convert multibyte character to corresponding wide character
strtod, wcstod Convert string to double
strtol, wcstol Convert string to long integer
strtoul, wcstoul Convert string to unsigned long integer
strxfrm, wcsxfrm Transform string into collated form based on locale-specific information
__toascii Convert character to ASCII code
tolower, towlower, _mbctolower Test character and convert to lowercase if currently uppercase
_tolower Convert character to lowercase unconditionally
toupper, towupper, _mbctoupper Test character and convert to uppercase if currently lowercase
_toupper Convert character to uppercase unconditionally
_ultoa, _ultow Convert unsigned long to string
wcstombs Convert sequence of wide characters to corresponding sequence of multibyte characters
wctomb Convert wide character to corresponding multibyte character
_wtoi Convert wide-character string to int
_wtol Convert wide-character string to long
- 作者: robindy 2005年04月28日, 星期四 09:24 加入博采
以下是一些可能用到的函数:
atoi atol atof
strtol strtoul strtod
以下代码演示了前三个函数的用法:
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
}
以下代码演示了后三个函数的用法:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char *string, *stopstring;
double x;
long l;
int base;
unsigned long ul;
string = "3.1415926This stopped it";
x = strtod( string, &stopstring );
printf( "string = %s/n", string );
printf(" strtod = %f/n", x );
printf(" Stopped scan at: %s/n/n", stopstring );
string = "-10110134932This stopped it";
l = strtol( string, &stopstring, 10 );
printf( "string = %s", string );
printf(" strtol = %ld", l );
printf(" Stopped scan at: %s", stopstring );
string = "10110134932";
printf( "string = %s/n", string );
/* Convert string using base 2, 4, and 8: */
for( base = 2; base <= 8; base *= 2 )
{
/* Convert the string: */
ul = strtoul( string, &stopstring, base );
printf( " strtol = %ld (base %d)/n", ul, base );
printf( " Stopped scan at: %s/n", stopstring );
}
return 0;
}
更详细的数据转换函数如下:
abs Find absolute value of integer
atof Convert string to float
atoi, _atoi64 Convert string to int
atol Convert string to long
_ecvt Convert double to string of specified length
_fcvt Convert double to string with specified number of digits following decimal point
_gcvt Convert double number to string; store string in buffer
_itoa, _i64toa, _itow, _i64tow Convert int to string
labs Find absolute value of long integer
_ltoa, _ltow Convert long to string
_mbbtombc Convert 1-byte multibyte character to corresponding 2-byte multibyte character
_mbcjistojms Convert Japan Industry Standard (JIS) character to Japan Microsoft (JMS) character
_mbcjmstojis Convert JMS character to JIS character
_mbctohira Convert multibyte character to 1-byte hiragana code
_mbctokata Convert multibyte character to 1-byte katakana code
_mbctombb Convert 2-byte multibyte character to corresponding 1-byte multibyte character
mbstowcs Convert sequence of multibyte characters to corresponding sequence of wide characters
mbtowc Convert multibyte character to corresponding wide character
strtod, wcstod Convert string to double
strtol, wcstol Convert string to long integer
strtoul, wcstoul Convert string to unsigned long integer
strxfrm, wcsxfrm Transform string into collated form based on locale-specific information
__toascii Convert character to ASCII code
tolower, towlower, _mbctolower Test character and convert to lowercase if currently uppercase
_tolower Convert character to lowercase unconditionally
toupper, towupper, _mbctoupper Test character and convert to uppercase if currently lowercase
_toupper Convert character to uppercase unconditionally
_ultoa, _ultow Convert unsigned long to string
wcstombs Convert sequence of wide characters to corresponding sequence of multibyte characters
wctomb Convert wide character to corresponding multibyte character
_wtoi Convert wide-character string to int
_wtol Convert wide-character string to long
- 作者: robindy 2005年04月28日, 星期四 09:24 加入博采
Trackback
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=1352094相关文章推荐
- 【原】如何将十六进制字符串转换为数字(数值)
- MySQL如何有效的存储IP地址及字符串IP和数值之间如何转换
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南)
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南)
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南MSDN)
- Swift如何将数值转换为可拼写的字符串
- 如何正确实现PHP字符串转换为数值的需求
- 如何:在十六进制字符串与数值类型之间转换(C# 编程指南)
- Swift如何将数值转换为可拼写的字符串
- 如何将十六进制字符串转换为数字(数值)
- 如何:在十六进制字符串与数值类型之间转换
- JavaScript 如何将字符串转换为数值
- 如何转换字符串为数值- -
- Swift如何将数值转换为可拼写的字符串
- 如何:在十六进制字符串与数值类型之间转换
- 如何将一段字符串转换为HTML格式
- 重温delphi之:如何将Bitmap位图与base64字符串相互转换
- 如何实现把gb2312的字符串编码转换成iso-8859-1的字符串
- 如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06)
- 如何将json字符串转换为对象