簡體   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