【问题标题】:could you explain the reasons about output when using std::hex and std::wios::hex in C++你能解释一下在 C++ 中使用 std::hex 和 std::wios::hex 时输出的原因吗
【发布时间】:2013-07-03 04:02:42
【问题描述】:

我有三个程序。 程序A的代码如下:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <locale>
using namespace std;
int _tmain( void )
{
    wstringstream s2;    
    TCHAR waTemp2[4] = {0xA0, 0xA1, 0x00A2, 0xA3};
    for (int i = 0; i < 4; i++)
    {
    s2<< hex <<(unsigned int)waTemp2[i] << " ";  
    }
    wstring strData2 =  s2.str();
    wcout << strData2.c_str() <<endl; 
    return 0;
}

这是输出:

a0 a1 a2 a3

programB的代码如下:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <locale>
using namespace std;
int _tmain( void )
{
    wstringstream s2;    
    TCHAR waTemp2[4] = {0xA0, 0xA1, 0x00A2, 0xA3};
    for (int i = 0; i < 4; i++)
    {
    s2<< hex << waTemp2[i] << " ";  
    }
    wstring strData2 =  s2.str();
    wcout << strData2.c_str() <<endl; 
    return 0;
}

这是输出:

??????

程序C代码如下:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <locale>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    wstringstream s2;    
    TCHAR waTemp2[4] = {0xA0, 0xA1, 0x00A2, 0xA3};
    for (int i = 0; i < 4; i++)
    {
    s2 << std::wios::hex <<(unsigned int)waTemp2[i] << " "; 
    }
    wstring strData2 =  s2.str();
    wcout<< strData2.c_str() <<endl;
    return 0;
}

这是输出:

2048160 2048161 2048162 2048163

你能告诉我std::wios::hex和std::hex,std::hex

非常感谢!

【问题讨论】:

    标签: c++ hex difference


    【解决方案1】:

    std::hex 是一个操纵者。当您传递整数时,它将流设置为输出十六进制。相当于在流上调用setf(std::wios::hex, std::wios::basefield);(假设是宽流)。例如,尝试对您的代码进行以下修改。您应该会看到相同的结果。

    wchar_t waTemp2[4] = {0xA0, 0xA1, 0x00A2, 0xA3};
    s2.setf(std::wios::hex, std::wios::basefield);
    for (int i = 0; i < 4; i++)
    {
        s2 << (unsigned)waTemp2[i] << " ";  
    }
    

    std::wios::hex 是用作位掩码标志的常数。不要将它与设置流的操纵器混淆。 在coliru 上,例如以下打印8

    std::cout << std::wios::hex;
    

    它用作位掩码来更新流上的格式标志。它将定义如下内容(参见 libstdc++ here 中的真实定义):

    enum fmtflags
    {
       _hex = 1L << 3,
    };
    
    class ios_base
    {
        static const fmtflags hex = _hex;
    };
    

    您看到2048160 2048161 2048162 2048163 的原因是它只是打印出std::wios::hex(unsigned int)waTemp2[i] 的数字。中间加空格可以看到s2 &lt;&lt; std::wios::hex &lt;&lt; " " &lt;&lt; (unsigned int)waTemp2[i] &lt;&lt; " ";

    s2 &lt;&lt; hex &lt;&lt; waTemp2[i] &lt;&lt; " "; 的问题是std::hex 仅用于整数。由于wchar_t 不是整数,它只是打印对应的字符。

    【讨论】:

    • @yaoike:这两个问题是什么?
    • 非常感谢。我还有三个问题。 1. 你说 std::hex 是一个操纵器。当您传递整数时,它将流设置为输出十六进制。为什么当我传递一个整数而不是 TCHAR 时它会起作用,我在 C++ 参考link 中找不到 std::hex 操纵器可以采用整数。 2. 编码s2&lt;&lt; hex &lt;&lt;(unsigned int)waTemp2[i] &lt;&lt; " ";,有什么办法可以代替ios_base&amp; hex (ios_base&amp; str);表格
    • 3.你说的这句话我没看懂。“但是,结果是不确定的,因为0xA0等大于值std::wios::hex中的位数.", 值 std::wios::hex 中的位数是否等于 64?当我使用 0x05 替换 0xA0 时它不起作用,你能举个例子吗?
    • @yaoike:1)您的链接清楚地表明:**integral numerical values** inserted into the stream are expressed in hexadecimal base 。 2)我不确定你在问什么,你的意思是像函数std::hex(std::wcout);一样调用它吗? 3)对不起,我弄错了,不是按位左移,它只是打印数字。例如,2048160 中的160 只是十进制的0xA0,其他的也是如此(请参阅我的更新答案)。
    • 2) 是的。 3)好的,我看到的是2048 160 2048 161 2048 162 2048 163,但是为什么是2048而不是8,你说:@987654345 @
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多