C++ 如何将 ASCII 字符的十六进制值写入文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/623784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:21:04  来源:igfitidea点击:

How does one write the hex values of a char in ASCII to a text file?

c++asciihexofstream

提问by xian

Here is what I currently have so far:

这是我目前所拥有的:

void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix )
{
    unsigned char *buf = (unsigned char*)ptr;

    for( int i = 0; i < buflen; ++i ) {
        if( i % 16 == 0 ) {
            stream << prefix;
        }

        stream << buf[i] << ' ';
    }
}

I've tried doing stream.hex, stream.setf( std::ios::hex ), as well as searching Google for a bit. I've also tried:

我试过做 stream.hex、stream.setf( std::ios::hex ),以及在谷歌上搜索一下。我也试过:

stream << stream.hex << (int)buf[i] << ' ';

But that doesn't seem to work either.

但这似乎也不起作用。

Here is an example of some output that it currently produces:

这是它当前生成的一些输出的示例:

í í í í í í í í í í í í í í í í 
í í í í í í í í í í í í í í í í 
í í í í í í í í í í í í í í í í 
í í í í í í í í í í í í í í í í 
í í í í í í í í í í í í í í í í 
í í í í í í í í í í í í í í í í 

I would like the output to look like the following:

我希望输出如下所示:

FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00

回答by

#include <iostream>
using namespace std;
int main() {
    char c = 123;
    cout << hex << int(c) << endl;
}

Edit:with zero padding:

编辑:零填充:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    char c = 13;
    cout << hex << setw(2) << setfill('0') << int(c) << endl;
}

回答by McDowell

char upperToHex(int byteVal)
{
    int i = (byteVal & 0xF0) >> 4;
    return nibbleToHex(i);
}

char lowerToHex(int byteVal)
{
    int i = (byteVal & 0x0F);
    return nibbleToHex(i);
}

char nibbleToHex(int nibble)
{
    const int ascii_zero = 48;
    const int ascii_a = 65;

    if((nibble >= 0) && (nibble <= 9))
    {
        return (char) (nibble + ascii_zero);
    }
    if((nibble >= 10) && (nibble <= 15))
    {
        return (char) (nibble - 10 + ascii_a);
    }
    return '?';
}

More code here.

更多代码在这里

回答by Martin York

Try:

尝试:

#include <iomanip>
....
stream << std::hex << static_cast<int>(buf[i]);

回答by Mnebuerquo

You can also do it using something a bit more old-fashioned:

你也可以使用更老式的东西来做到这一点:

char buffer[4];//room for 2 hex digits, one extra ' ' and 
wchar_t* CharToWstring(CHAR Character)
{
wchar_t TargetString[10];
swprintf_s(TargetString, L"%02X", Character);

// then cut off the extra characters
size_t Length = wcslen(TargetString);

wchar_t *r = new wchar_t[3];
r[0] = TargetString[Length-2];
r[1] = TargetString[Length-1];
r[2] = '
void CharToHex(char c, char *Hex)
{
   Hex[0]=HexDigit(c>>4);
   Hex[1]=HexDigit(c&0xF);
}

char HexDigit(char c)
{
   if(c<10)
     return c;
   else
     return c-10+'A';
}
'; return r; }
sprintf(buffer,"%02X ",onebyte);

回答by Alatey

CHAR to wchar_t (unicode) HEX string

CHAR 到 wchar_t (unicode) HEX 字符串

stream << std::hex << std::setfill('0') << std::setw(2)

回答by SurDin

I usually make a function which returns the digits and just use it:

我通常制作一个返回数字的函数并使用它:

##代码##

回答by Edouard A.

You simply need to configure your stream once:

您只需要配置一次流:

##代码##