c 中string和int相互轉換

2021-06-27 02:27:03 字數 622 閱讀 8669

有兩種方法

1. c++中string到int的轉換

1) 在c標準庫裡面,使用atoi:

#include #include std::string text = "152";

int number = std::atoi( text.c_str() );

if (errno == erange) //可能是std::errno

else if (errno == ????)

//可能是einval

2) 在c++標準庫裡面,使用stringstream:(stringstream 可以用於各種資料型別之間的轉換)

#include #include std::string text = "152";

int number;

std::stringstream ss;

ss << text;//可以是其他資料型別

ss >> number; //string -> int

if (! ss.good())

ss << number;// int->string

string str = ss.str();

if (! ss.good())

c 中string和int的相互轉化

在c 中有時候需要對資料進行型別轉化,今天我們來看一下c 中string與int相互轉化的方法 1.int轉string c 11標準增加了全域性函式std to string string to string int val string to string long val string to ...

Java中String和int相互轉換

1 如何將字串 string 轉換成整數 int?a.有兩個方法 1 int i integer.parseint string 或 i integer.parseint string int radix 2 int i integer.valueof my str intvalue 注 字串轉成 ...

Int 和String的相互轉換

string s 100 string integer 字串轉換為整形型別 integer i integer.valueof s int x i.intvalue system.out.println x 方式2靜態方式更方便使用 public static int parseint s int ...