繁体   English   中英

十六进制输出到字符串c++

[英]HEX output to String c++

我正在尝试将printf输出转换为std::string变量。 我有以下 for 循环:

for(i=0; i<53; i++) {
    printf("%02X", pbRecvBuffer[i]);
}

此循环的输出是十六进制值,例如: 01445420434F2.. 我的第一次尝试是使用stringstream如下:

stringstream os;

for(i=0; i<53; i++) 
    os << std::hex << pbRecvBuffer[i];

std::cout << os << std::endl;

不幸的是,这给了我错误的结果。 也许有人直接看到它可以帮助我。

通常,例如十六进制和整数流是一个好主意:

std::ios_base::fmtflags f(out.flags()); // Init

out <<std::hex << std::setfill('0') << std::setw(2) << (unsigned int)pbRecvBuffer[i];

out.flags(f); // restore

一般 out 是从 std::cout、std::ostringstream、std::basic_ostream 等派生的对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM