stringstream - 如何让 stringstream 将 uint8_t 视为数字而不是字符?

标签 stringstream

我有这个代码,想知道是否有可能让 stringstream 将 uint8_t 视为数字而不是字符?

uint8_t s;
std::stringstream sstream( "255" );
sstream >> s;
std::cout << s << " equals 50/'2' not 255 " << std::endl;

s 应该是 255 而不是 50/'2'

最佳答案

如果您使用 std::stringstream 将 uint8_t 转换为字符串,那么您可以使用 std::to_string反而。仅在 c++11 中允许。

C++11 特性

#include <stdint.h>
#include <iostream>
uint8_t value = 7;
std::cout << std::to_string(value) << std::endl;
// Output is "7"

关于stringstream - 如何让 stringstream 将 uint8_t 视为数字而不是字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047028/

相关文章:

c++ - 使用 std::setw 后从流输出时如何清除宽度?

c++ - std::stringstream 作为参数

c# - C# 中的字符串流

C++:如何构建两个空格分隔字符串的交集字符串?

c++ - 将字符串流解析为字符串和 double

c++ - 为什么这段代码没有给出期望的输出?

c++ - 如何将二进制文件的十六进制表示形式保存到 std::string 中?

c++ - stringstream 删除前导 0

c++ - 从 std::stringstream 检索 char

c++ - 从字符串流到字符串的转换删除了 '=' 个字符