【问题标题】:How does one write the hex values of a char in ASCII to a text file?如何将 ASCII 字符的十六进制值写入文本文件?
【发布时间】:2023-09-13 07:46:01
【问题描述】:

这是我目前所拥有的:

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] << ' ';
    }
}

我尝试过使用 stream.hex、stream.setf(std::ios::hex),以及在 Google 上搜索了一下。我也试过:

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

但这似乎也不起作用。

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

Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í 

我希望输出如下所示:

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

【问题讨论】:

  • 顺便说一句:你应该使用 const void *ptr 和 const char *prefix 来明确你不会修改这些缓冲区。
  • 这就是我如此喜欢堆栈溢出的原因。这些有趣的小问题时不时会出现,有人投了一个sn-p代码并解决了......

标签: c++ ascii hex ofstream


【解决方案1】:
#include <iostream>
using namespace std;
int main() {
    char c = 123;
    cout << hex << int(c) << endl;
}

编辑:零填充:

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

【讨论】:

  • @kitchen - 将 cout 替换为您自己的流。
  • @rob the hex manipul;ator 不是流类的成员。您正在打印的十六进制实际上是一个枚举值
  • 如果 c
  • 这就是我喜欢 C++流的原因: cout so 比 printf("%02x", c) 清晰得多 ;-) 我知道它的类型安全(呃),但他们难道不能想出一些不那么怪诞的东西吗?
  • 清晰是观点的问题...我不认为 printf("%02x", c) 可以被认为是“清晰”
【解决方案2】:
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 '?';
}

更多代码here.

【讨论】:

    【解决方案3】:

    试试:

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

    【讨论】:

      【解决方案4】:

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

      char buffer[4];//room for 2 hex digits, one extra ' ' and \0
      sprintf(buffer,"%02X ",onebyte);
      

      【讨论】:

        【解决方案5】:

        我通常会创建一个返回数字的函数并使用它:

        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';
        }
        

        【讨论】:

          【解决方案6】:

          CHAR 到 wchar_t (unicode) HEX 字符串

          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] = '\0';
          return r;
          }
          

          【讨论】:

            【解决方案7】:

            您只需配置一次流:

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

            【讨论】:

            • -1 This is wrong。两年多来,它是如何避免被否决的?!
            最近更新 更多