在C ++中将String转换为int /将int转换为String

This EE article provides examples of conversion techniques for various types of string variables:  classic C-style strings, MFC/ATL CStrings, and STL's std::string data type.

这篇EE文章提供了各种类型的字符串变量转换技术的示例:经典的C样式字符串,MFC / ATL CString和STL的std :: string数据类型。

将整数转换为字符串 (Convert Integer to String)

int to C-style (NULL-terminated char[]) string

int   n=1234567890;
char  szResult[12];

#include 
...
sprintf(   szResult, "%d", n );               // use "%u" for unsigned int
sprintf_s( szResult, sizeof(szResult), "%d", n );  // "safe" version

//------------------------------------- alternative: itoa
#include 
...
_itoa(   n, szResult, 10 );
_itoa_s( n, szResult, sizeof(szResult), 10);    // "safe" version

int to ATL/MFC CString

#include 
...
int     n;
CString sResult;

sResult.Format( "%d", n );    // use "%u" for unsigned int

int to STL std::string

#include 
#include 
...
int         n;
std::string sResult;

std::ostringstream ostr;
ostr << n;
sResult= ostr.str();

将字符串转换为整数 (Convert String to Integer)

C-style (NULL-terminated char[]) string to int

将C样式(以NULL结尾的

#include 
...
char szNumber[12]= "1234567890";
int  nResult;

nResult= atoi( szNumber );

ATL/MFC CString to int

ATL / MFC

#include   
...
CString sNumber= "1234567890";
int     nResult;

nResult= atoi( sNumber );  // automatically does LPCSTR(sNumber)

STL std::string to int

STL

#include 
#include 
...
std::string sNumber= "1234567890";
int         nResult;

nResult= atoi( sNumber.c_str() );

Notes:

笔记:

If you are using the UNICODE character set and supplying an output buffer (as with

如果您正在使用UNICODE字符集并提供输出缓冲区(如

sprintf and itoa) you'll need to keep in mind that characters are two bytes long.  The normal procedure is to declare character buffers as the TCHAR data type, which will take into consideration the data element size. TCHAR数据类型,这将考虑数据元素的大小。

Output buffer length. 输出缓冲区长度。

C++ integers are typically 32-bits, with values as high as billions; the range is: 0 to 4,294,967,295 (unsigned) -2,147,483,648 to 2,147,483,647 (signed)Thus, the maximum length of the resulting string is 12 characters (including NULL terminator and not including commas or other formatting).

C ++整数通常为32位,其值高达十亿 ; 范围是:0到4,294,967,295(无符号)-2,147,483,648到2,147,483,647(有符号)因此,结果字符串的最大长度为12个字符(包括NULL终止符,不包括逗号或其他格式)。



If you are working with 64-bit integers (called __int64 or long long), the values are as high as quintillions; the range is: 0 to 18,446,744,073,709,551,615 (unsigned) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed)Thus, the maximum length of the resulting string is 21 characters (including NULL terminator and not including commas or other formatting).

如果您使用的是64位整数(称为__int64long long ),则该值高达quintillions ; 范围是:0到18,446,744,073,709,551,615(无符号)-9,223,372,036,854,775,808到9,223,372,036,854,775,807(有符号)因此,生成的字符串的最大长度为21个字符(包括NULL终止符且不包括逗号或其他格式)。

Both

sprintf and sprintfitoa have been termed itoa被称为 unsafe (some would say they've been deprecated, others would not use that term) because of the chance that a sloppy programmer might not provide a large enough output buffer.

The examples show how to use the safer xxx_s variations of these functions as an alternative.  The older functions might write beyond the end of the buffer and stomp on other variables or blow the stack frame -- and cause endless debugging headaches.  Of course, if you are determined to give yourself grief, you can still blow up the "safe" version by passing in the wrong length value...  

这些示例说明了如何使用这些功能的更安全的_s替代形式。 较旧的函数可能会超出缓冲区的末尾写入,并踩到其他变量或破坏堆栈帧-并引起无尽的调试麻烦。 当然,如果您决定让自己感到悲伤,您仍然可以通过传递错误的长度值来炸毁“安全”版本...



The CString::Format function allocates the buffer for you and takes care to avoid the buffer overrun problem.  The std::ostringstream << (insertion operator) also takes care of the buffer allocation for you.

CString :: Format函数为您分配缓冲区,并注意避免缓冲区溢出问题。 std :: ostringstream << (插入运算符)还为您处理缓冲区分配。

The examples above compile and work under Microsoft VS2008.  Some Microsoft-specific functionality is implied (refer to the references, below, if you worry about these things).  However, there is an excellent chance that at least one of the variations will work for you in your development system, whatever it is.

以上示例可在Microsoft VS2008下编译和工作。 隐含某些特定于Microsoft的功能(如果您担心这些事情,请参考下面的参考资料)。 但是,很有可能至少有一种变体将在您的开发系统中为您起作用,无论它是什么。

References:参考文献:

atoi, _atoi_l, _wtoi, _wtoi_l

atoi,_atoi_l,_wtoi,_wtoi_l

http://msdn.microsoft.com/en-us/library/yd5xkb5c.aspx http://msdn.microsoft.com/zh-CN/library/yd5xkb5c.aspx

sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

sprintf,_sprintf_l,swprintf,_swprintf_l,__ swprintf_l

http://msdn.microsoft.com/en-us/library/ybk95axf.aspx http://msdn.microsoft.com/zh-CN/library/ybk95axf.aspx

_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow

_itoa,_i64toa,_ui64toa,_itow,_i64tow,_ui64tow

http://msdn.microsoft.com/en-us/library/yakksftt(VS.80).aspx http://msdn.microsoft.com/zh-CN/library/yakksftt(VS.80).aspx

String to Numeric Value Functions  (strtoX, et al.)

字符串到数值函数strto 等)

http://msdn.microsoft.com/en-us/library/53b7b72e(VS.80).aspx http://msdn.microsoft.com/zh-CN/library/53b7b72e(VS.80).aspx

Data Conversion  (ultoX, etc.)

数据转换ulto 等)

http://msdn.microsoft.com/en-us/library/0heszx3w(VS.80).aspx http://msdn.microsoft.com/zh-cn/library/0heszx3w(VS.80).aspx

CStringT::Format

CStringT :: Format

http://msdn.microsoft.com/en-us/library/aa314327(VS.60).aspx http://msdn.microsoft.com/zh-CN/library/aa314327(VS.60).aspx

ostringstream

串流

http://msdn.microsoft.com/en-us/library/6kacs5y3.aspx http://msdn.microsoft.com/zh-CN/library/6kacs5y3.aspx

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=

If you liked this article and want to see more from this author,  please click the Yes button near the:

如果您喜欢这篇文章,并希望从该作者那里获得更多信息,请单击旁边的按钮:

      Was this article helpful?

本文是否有帮助?

label that is just below and to the right of this text.   Thanks!

此文字下方和右侧的标签。

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=-=- =-=-=-=

翻译自: https://www.experts-exchange.com/articles/1577/Convert-String-to-int-Convert-int-to-String-in-C.html

你可能感兴趣的:(字符串,c++,python,java,编程语言)