C++以十六进制值打印十进制值。

huangapple go评论86阅读模式
英文:

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 ?

C++以十六进制值打印十进制值。

int ia = (int)A;
cout &lt;&lt; left &lt;&lt; &quot;0x&quot; &lt;&lt; setw(sizeof(ia)*2) &lt;&lt; hex &lt;&lt; uppercase &lt;&lt; ia &lt;&lt; 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是填充字符。<是左对齐符号。#在数字开头添加0XX是用于大写十六进制整数格式化的格式说明符。


<details>
<summary>英文:</summary>

I believe that what you want is something like this:

    #include &lt;iostream&gt;
    #include &lt;iomanip&gt;
    #include &lt;limits&gt;
    
    int main()
    {
      int ia = 42;
      const auto save_flags = std::cout.flags();
      std::cout
        &lt;&lt; std::left
        &lt;&lt; std::showbase
        &lt;&lt; std::hex
        &lt;&lt; std::uppercase
        &lt;&lt; std::setfill(&#39;0&#39;)
        &lt;&lt; std::setw(std::numeric_limits&lt;decltype(ia)&gt;::digits / 4)
        &lt;&lt; ia
        &lt;&lt; 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(&#39;0&#39;)` up to your desired width (which I made the maximum number of digits for the given type - the `std::setw(std::numeric_limits&lt;decltype(ia)&gt;::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( &quot;{:0&lt;#{}X}\n&quot;, ia,
            std::numeric_limits&lt;decltype( ia )&gt;::digits / 4 );

}

Output:
```shell
0X2A000

Explanation: Here in &quot;{:0&lt;#{}X}\n&quot;, 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 &lt;iostream&gt;
#include&lt;iomanip&gt;

int main() {

    int ia = (int)100;
    std::cout &lt;&lt; std::left &lt;&lt; &quot;0x&quot; &lt;&lt; std::hex &lt;&lt; ia &lt;&lt; std::endl;

    return 0;
}

huangapple
  • 本文由 发表于 2023年3月9日 18:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定