将char的向量转换为int [英] convert vector of char to an int

查看:90
本文介绍了将char的向量转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 char s的 vector s的一部分转换为 int

如果我有

I need to convert a part of vectors of chars to an int.
If I have

std::vector<char> // which contains something like asdf1234dsdsd  

之类的东西,我想从第4位到第7位获取字符并进行转换

位置总是已知的。

and I want to take characters from the 4th till 7th position and convert it into an int.
The positions are always known.

我如何最快地做到这一点?

How do I do it the fastest way ?

我尝试使用全局副本并得到了一个奇怪的答案。而不是2,我得到了48。

I tried to use global copy and got a weird answer. Instead of 2 I got 48.

推荐答案

如果位置已知,您可以这样做

If the positions are known, you can do it like this

int x = (c[4] - '0') * 1000 + (c[5] - '0') * 100 + (c[6] - '0') * 10 + c[7] - '0';

这篇关于将char的向量转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆