std::string 点分十进制
时间: 2023-09-10 19:06:13 浏览: 45
std::string 是 C++ 标准库中的一个类,它提供了对字符串的操作和管理。在 C 语言中,字符串是以 null 结尾的字符数组,而使用 std::string 可以克服这一缺点。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【C++标准库】std::string用法指南&源码剖析](https://blog.csdn.net/u011436427/article/details/125902935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关问题
std::string将字符串转换成字符中字符所代表的十进制数据
如果你想要将std::string中的字符转换为其所代表的十进制数据,你可以使用std::stoi或std::stol函数。这些函数可以将字符串转换为整数或长整数。
下面是一个示例:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "12345";
for (char c : str) {
int decimalValue = std::stoi(std::string(1, c));
std::cout << decimalValue << " ";
}
return 0;
}
```
输出结果为:
```
1 2 3 4 5
```
在上面的示例中,我们遍历了字符串中的每个字符,并使用std::stoi函数将其转换为整数。注意,我们将字符c转换为一个长度为1的字符串,然后将其作为参数传递给std::stoi函数。这样可以确保将字符转换为对应的十进制数据。
修改 n = sp.read(dataBuffer, n); for(int i = 0; i < n; i++) { hexArray[i] = std::stoi(std::to_string(dataBuffer[i]),0,16); }使得hexArray以i十进制存储
要将 hexArray 数组以十进制存储,可以将上述代码中的 std::stoi() 函数改为 std::atoi() 函数,因为 std::atoi() 函数可以将字符串解析成十进制数。修改后的代码如下:
```c++
n = sp.read(dataBuffer, n);
for(int i = 0; i < n; i++) {
hexArray[i] = std::atoi(std::to_string(dataBuffer[i]).c_str());
}
```
其中,std::to_string() 函数仍然将字节数据转换成字符串,然后使用 c_str() 函数将字符串转换成 C 风格字符串,最后使用 std::atoi() 函数将 C 风格字符串解析成十进制数,并将结果保存在 hexArray 数组中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)