gpt4 book ai didi

c++ - 对齐旁边的十六进制数

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:35 24 4
gpt4 key购买 nike

我需要一些脚本方面的帮助。我有十六进制数字,我需要在它们旁边对齐,但我不知道如何用 C++ 中的好方法解决这个问题。

例如我有这个数组:

int test[3]={0x12,0x13,0xab};

所以我想得到这个输出:

0x1213ab

最佳答案

你可以轻松做到这一点

#include <iostream>
#include <iomanip>

int main() {
int test[3] = {0x12,0x13,0xab};

std::cout << "0x";
for(auto x : test) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << x;
// | | |
// | | v
// | | Prevents filling blanks
// | v
// | Chooses a field output size of 2
// v
// Provides hex formatting of integers
}
std::cout << std::endl;
}

参见 Live Demo


相关链接来自好c++ reference documentation :

关于c++ - 对齐旁边的十六进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353962/

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