[英]C++ cout hex values?
我想要做:
int a = 255;
cout << a;
並讓它在 output 中顯示 FF,我該怎么做?
std::hex
定義在<ios>
,它包含在<iostream>
。 但是要使用std::setprecision/std::setw/std::setfill
/ std::setprecision/std::setw/std::setfill
/etc 之類的東西,您必須包含<iomanip>
。
要操作流以十六進制打印,請使用hex
操作符:
cout << hex << a;
默認情況下,十六進制字符以小寫形式輸出。 要將其更改為大寫,請使用uppercase
操作符:
cout << hex << uppercase << a;
稍后要將輸出更改回小寫,請使用nouppercase
操作nouppercase
:
cout << nouppercase << b;
如果要打印單個十六進制數,然后恢復為十進制數,可以使用以下命令:
std::cout << std::hex << num << std::dec << std::endl;
std::hex
您提供十六進制格式,但它是一個有狀態的選項,這意味着您需要保存和恢復狀態,否則它將影響所有未來的輸出。
天真地切換回std::dec
只有在標志之前的位置時才有好處,但情況可能並非如此,尤其是在您編寫庫時。
#include <iostream>
#include <ios>
...
std::ios_base::fmtflags f( cout.flags() ); // save flags state
std::cout << std::hex << a;
cout.flags( f ); // restore flags state
這結合了 Greg Hewgill 的回答和來自另一個問題的信息。
我知道這不是 OP 所要求的,但我仍然認為值得指出如何使用 printf 來做到這一點。 我幾乎總是喜歡使用它而不是 std::cout (即使沒有以前的 C 背景)。
printf("%.2X", a);
'2' 定義精度,'X' 或 'x' 定義大小寫。
您也可以使用不同種類的標志和面具。 有關更多信息,請參閱http://www.cplusplus.com/reference/iostream/ios_base/setf/ 。
#include <iostream>
using namespace std;
int main()
{
int num = 255;
cout.setf(ios::hex, ios::basefield);
cout << "Hex: " << num << endl;
cout.unsetf(ios::hex);
cout << "Original format: " << num << endl;
return 0;
}
使用std::uppercase
和std::hex
將整數變量a
格式化a
以十六進制格式顯示。
#include <iostream>
int main() {
int a = 255;
// Formatting Integer
std::cout << std::uppercase << std::hex << a << std::endl; // Output: FF
std::cout << std::showbase << std::hex << a << std::endl; // Output: 0XFF
std::cout << std::nouppercase << std::showbase << std::hex << a << std::endl; // Output: 0xff
return 0;
}
C++20 std::format
在我看來,這是現在最干凈的方法,因為它不會用std::hex
污染std::cout
狀態:
主程序
#include <format>
#include <string>
int main() {
std::cout << std::format("{:x} {:#x} {}\n", 16, 17, 18);
}
預期輸出:
10 0x11 18
尚未在 GCC 10.0.1、Ubuntu 20.04 上實現。
但是成為 C++20 的令人敬畏的庫在安裝后應該是相同的:
git clone https://github.com/fmtlib/fmt
cd fmt
git checkout 061e364b25b5e5ca7cf50dd25282892922375ddc
mkdir build
cmake ..
sudo make install
main2.cpp
#include <fmt/core.h>
#include <iostream>
int main() {
std::cout << fmt::format("{:x} {:#x} {}\n", 16, 17, 18);
}
編譯並運行:
g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main2.out main2.cpp -lfmt
./main2.out
記錄在:
更多信息請訪問: std::string 格式,如 sprintf
Pre-C++20:干凈地打印並將std::cout
恢復到以前的狀態
主程序
#include <iostream>
#include <string>
int main() {
std::ios oldState(nullptr);
oldState.copyfmt(std::cout);
std::cout << std::hex;
std::cout << 16 << std::endl;
std::cout.copyfmt(oldState);
std::cout << 17 << std::endl;
}
編譯並運行:
g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp
./main.out
輸出:
10
17
更多細節:操作后恢復 std::cout 的狀態
在 GCC 10.0.1、Ubuntu 20.04 上測試。
你好嗎!
#include <iostream>
#include <iomanip>
unsigned char buf0[] = {4, 85, 250, 206};
for (int i = 0;i < sizeof buf0 / sizeof buf0[0]; i++) {
std::cout << std::setfill('0')
<< std::setw(2)
<< std::uppercase
<< std::hex << (0xFF & buf0[i]) << " ";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.