英文:
C++ printing out a decimal value as hex value
问题
int ia = (int)A;
cout << left << "0x" << setw(sizeof(ia) * 2) << hex << uppercase << ia << endl;
英文:
I am trying to write an integer decimal value as hex value in C++, but the code below writes the hex value seperated( see the attached image). I want to write it on left and not-seperated. For example if my integer value is 100, its hex value is 0x64 and i want to print out the hex value. Any advise ?
int ia = (int)A;
cout << left << "0x" << setw(sizeof(ia)*2) << hex << uppercase << ia << endl;
I am trying to write an integer value with hex value in C++, but the code below writes the hex value seperated( see the attached image). I want to write it on left and not-seperated.
答案1
得分: 1
我相信你想要的是这样的:
```cpp
#include <iostream>
#include <iomanip>
#include <limits>
int main()
{
int ia = 42;
const auto save_flags = std::cout.flags();
std::cout
<< std::left
<< std::showbase
<< std::hex
<< std::uppercase
<< std::setfill('0')
<< std::setw(std::numeric_limits<decltype(ia)>::digits / 4)
<< ia
<< std::endl;
std::cout.flags(save_flags);
}
这将首先保存std::cout
流的现有标志,以便在操纵它们后可以恢复它们。然后它会打印左对齐的数字(std::left
),并显示正在打印的数字的基数(十六进制的0X
- 这是std::showbase
位),它将以十六进制(std::hex
)且大写字母表示(std::uppercase
),并使用0
来填充任何缺少的数字(直到达到所需的宽度,我将其设置为给定类型的最大数字的位数的四分之一 - std::setw(std::numeric_limits<decltype(ia)>::digits / 4)
)。最后,我们恢复流的先前标志,以便将来的打印获取正常(或至少之前设置的)标志。
C++23
从C++23开始,您可以摆脱iostreams并使用std::print
函数进行格式化和打印。
#include <print>
#include <limits>
int main()
{
int ia { 42 };
std::print("{:0<#{}X}\n", ia,
std::numeric_limits<decltype(ia)>::digits / 4);
}
输出:
0X2A000
解释: 在"{:0<#{}X}\n"
中,0是填充字符。<是左对齐符号。#在数字开头添加0X。X是用于大写十六进制整数格式化的格式说明符。
<details>
<summary>英文:</summary>
I believe that what you want is something like this:
#include <iostream>
#include <iomanip>
#include <limits>
int main()
{
int ia = 42;
const auto save_flags = std::cout.flags();
std::cout
<< std::left
<< std::showbase
<< std::hex
<< std::uppercase
<< std::setfill('0')
<< std::setw(std::numeric_limits<decltype(ia)>::digits / 4)
<< ia
<< std::endl;
std::cout.flags(save_flags);
}
This will first of all save the existing flags of the `std::cout` stream so that you can restore them later after you have manipulated them. Then it will print the number left aligned (`std::left`) and it will show the base of the number you are printing (`0X` for hex - this is the `std::showbase` bit), it will print the number as hex (`std::hex`) in uppercase (`std::uppercase`) and it will use `0` to fill any missing digits (`std::setfill('0')` up to your desired width (which I made the maximum number of digits for the given type - the `std::setw(std::numeric_limits<decltype(ia)>::digits / 4)` bit). Finally we restore the previous flags of the stream so future prints get the normal (or at least previously set) flags.
## C++23 ##
Starting from C++23, you can get rid of iostreams and use the [`std::print`](https://en.cppreference.com/w/cpp/io/print) funtion for formatting and printing purposes.
#include <print>
#include <limits>
int main( )
{
int ia { 42 };
std::print( "{:0<#{}X}\n", ia,
std::numeric_limits<decltype( ia )>::digits / 4 );
}
Output:
```shell
0X2A000
Explanation: Here in "{:0<#{}X}\n"
, 0 is the fill character. < is the left alignment symbol. # adds 0X to the beginning of the number. And X is the format specifier for uppercase hex integer formatting.
答案2
得分: 0
A simple way to do this is to use setbase(16)
or its equivalent hex
.
#include <iostream>
#include <iomanip>
int main() {
int ia = (int)100;
std::cout << std::left << "0x" << std::hex << ia << std::endl;
return 0;
}
英文:
A simple way to do this is to use setbase(16)
or its equivalent hex
.
#include <iostream>
#include<iomanip>
int main() {
int ia = (int)100;
std::cout << std::left << "0x" << std::hex << ia << std::endl;
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论