开发者

C++ How to convert last 4 characters of array to an integer?

I know how to convert a character array containing numbers to an integer using iostream:

char[] ar = "1234";
int num;
ar >> num;

but how would I convert the last four characters of that array to an int?

char[] ar = "sl34nfoe11intk1234";
int num;
????;

Is there a way to point to an element in the array and start streaming from ther开发者_JAVA百科e?

Ideally I would start streaming from max array size - 4.


char* p = ar + strlen(ar) - 4;

Now p points to the '1' of "1234", and you can feed p into the stream.


char ar[] = "abc1234";
std::istringstream ss(ar + 3);
int n = 0;
ss >> n;

Better yet, use std::string:

std::string ar("abc1234");
std::istringstream ss(ar.substr(ar.size() - 4));


What about

char[] ar = "sl34nfoe11intk1234";
int num;
(ar + strlen(ar) - 4) >> num;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜