gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:48:25 35 4
gpt4 key购买 nike

我有这个代码,想知道是否有可能让 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com