【问题标题】:Convert Decimal value to Hexadecimal in Visual C++ [duplicate]在 Visual C++ 中将十进制值转换为十六进制 [重复]
【发布时间】:2014-06-25 21:13:51
【问题描述】:

如何在 Visual Studio C++ 中将十进制值转换为十六进制。

例如,我想将十进制值 125 转换为十六进制 7D。

我可以在 C# 中使用 string hexValue = decValue.ToString("X"); 做到这一点

我将如何在 Visual C++ 中执行相同的操作。

【问题讨论】:

    标签: c++ visual-studio-2010 hex type-conversion decimal


    【解决方案1】:

    使用std::stringstream 类来格式化数字,如下所示:

    std::stringstream sstr;
    sstr << std::hex << 32768;
    std::string hexValue = sstr.str();
    

    【讨论】:

    • 报错:stringstream not a member of std
    • stringstream#include &lt;sstream&gt; 中声明
    【解决方案2】:

    这有帮助吗?

    https://stackoverflow.com/a/20034349/2064516

    std::stringstream ss;
    ss  << hex_value ; // std::string hex_value
    ss >> std::hex >> decimal_value ; //int decimal_value
    
    std::cout << decimal_value ;
    

    【讨论】:

    • 这与请求的转换相反。
    猜你喜欢
    • 2013-06-16
    • 2017-12-30
    • 2011-08-04
    • 2017-07-31
    • 2013-07-20
    • 2012-06-17
    • 2014-02-05
    • 2021-06-26
    相关资源
    最近更新 更多