C++ 读取二进制文件后的奇怪数字

C++ 读取二进制文件后的奇怪数字,c++,hex,decimal,binaryfiles,16-bit,C++,Hex,Decimal,Binaryfiles,16 Bit,我想将十六进制32位数字写入二进制文件,然后使用reinterpret_cast读取其中一些数字,例如16位数字。我只读取16位,因为它决定了数据包的大小。代码中有一个示例。也许问题出在大端还是小端 #include <iostream> // std::cout #include <fstream> // std::ifstream #include <cstdint> #include <vector> void saveT

我想将十六进制32位数字写入二进制文件,然后使用reinterpret_cast读取其中一些数字,例如16位数字。我只读取16位,因为它决定了数据包的大小。代码中有一个示例。也许问题出在大端还是小端

#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <cstdint>
#include <vector>

void saveTestData(void)
{
  std::vector<std::uint_fast32_t> tab
  {
    0x50e5495c, 0xe7b50200, 0xbe6b2248, 0x08004510,
    0x015c2340, 0x0000ff11, 0x1567c0a8, 0x004cc0a8,
    0x003de290, 0xc35a0148, 0x00000000, 0x01200003,
    0x00620000, 0x01140002, 0x00010000, 0x8000ef40,
    0x22560003, 0xe0042150, 0x00006bbf, 0xd67c800f,
    0x5b5b0003, 0xe0032150, 0x00006bbf, 0xd67c8007,
    0x1b5d0003, 0xe0022150, 0x00006bbf, 0xd67c800a,
    0xab5d0023, 0xe0052150, 0x00006bbf, 0xd67c8011,
    0x8b5c6bbf, 0xd67c8c55, 0xaf896bbf, 0xd67c8c90,
    0x4f896bbf, 0xd67c8cd4, 0xef8a6bbf, 0xd67c8d0d,
    0x1f8a6bbf, 0xd67c8d43, 0x7f886bbf, 0xd67c8d8f,
    0x8f896bbf, 0xd67c8dc4, 0xcf886bbf, 0xd67c8e19,
    0x6f896bbf, 0xd67c8e4e, 0x1f8a6bbf, 0xd67c8e82,
    0xcf8a6bbf, 0xd67c8ed7, 0x4f896bbf, 0xd67c8f0c,
    0xef896bbf, 0xd67c8f4f, 0x8f896bbf, 0xd67c8f96,
    0xef8a6bbf, 0xd67c8fdb, 0xcf896bbf, 0xd67c9008,
    0xbf89000e, 0x80001006, 0xf0724646, 0xb45b0000,
    0x00004646, 0xb45b0000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00004646, 0xb45b0000,
    0x00004646, 0xb45b0000, 0x00008000, 0x00000001,
    0x55550000, 0x0001aaaa, 0xaaaa0000, 0x01200003,
    0x00620000, 0x01140002, 0x00010000, 0x8000ef40,
    0x22560003, 0xe0042150, 0x0000
  };

  std::ofstream file;
  file.open("test.omgwtf", std::ofstream::binary);
  if(file.good())
  {
    file.write(reinterpret_cast<char*>(tab.data()), tab.size()*sizeof(std::uint_fast32_t));
    file.close();
  }

}

int main()
{
  saveTestData();
  std::ifstream file("test.omgwtf", std::ifstream::binary);
  if(file.good())
  {
    file.seekg(0, file.end);
    uint32_t length = file.tellg();
    file.seekg(0, file.beg);

    char *buffer = new char[length];
    std::cout << "length = " << length << std::endl;
    file.read(buffer, length);

    std::uint_fast32_t *number32 = reinterpret_cast<std::uint_fast32_t*>(buffer);
    std::cout << "1 number32 = " << *number32 << std::endl;     // ok

    number32 = reinterpret_cast<std::uint_fast32_t*>(buffer+4);
    std::cout << "2 number32 = " << *number32 << std::endl;     // ok

    // read 0xbe6b (16 bits not 32)
    // 0xbe6b (hex) = 48747 (dec)
    std::uint_fast16_t *number16 = reinterpret_cast<std::uint_fast16_t*>(buffer+8);
    std::cout << "3 number16 = " << *number16 << std::endl;     // not ok!? why?

    // read 2248 (16 bits not 32)
    // 2248 (hex) = 8776 (dec)
    number16 = reinterpret_cast<std::uint_fast16_t*>(buffer+10);
    std::cout << "4 number16 = " << *number16 << std::endl;     // not ok!? why?

    file.close();
    delete [] buffer;
  }
  return 0;
}

std::binary
名称不好,它确实控制着换行符的转换

iostreams仅用于文本。您可以使用不带换行符翻译的文本(使用
std::binary
,文件以Unix换行符约定结束,
\n
仅限)或带换行符翻译的文本(不要使用
std::binary
,文件以遵循操作系统约定结束,例如
\n
\r\n
,甚至
\r

但即使使用
std::binary
,EOF字符(ASCII 26)也可能被识别并结束输入。或者不是。标准没有说。该标准没有为未翻译的文件访问提供任何机制

人们一直试图设计一个更好的C++标准I/O机制,它将文件访问和文本处理分开,但没有人能让每个人都满意。 对于二进制文件,使用低级I/O机制。即使
比iostreams更好(翻译更少),但它仍然有一些缺陷。特定于操作系统的函数,或使用下面的操作系统函数的跨平台包装器(如
boost::asio
)是二进制文件访问所需的

此外,到处都有严格的别名冲突。不要像那样使用
重新解释\u cast
,而是使用
memcpy
或从输入文件中单独读取正确大小的块

最后,您读取了错误的大小变量
uint\u fast16\t
不是16位,而是16位或更多,无论哪一位最快。几乎可以肯定的是,在CPU上,32位比16位快。如果您想要正好16位,请使用
uint16\u t
。如果您希望尽可能接近(但不能少于)使用
uint\u t至少16\u t
uint\u fast
类型系列适用于局部变量,如循环计数器。由于大小未知,它们对I/O没有用处


一旦你弄明白了所有这些,你就需要担心原始数据的Endiance了,因为它是以32位(或更多)值的序列来写的,高半部还是低半部先写入文件取决于平台。

使用
hextdump
xxd
显示实际写入文件的数据。
std::uint\u fast16\u t
不保证为16位。它可以更大<代码>标准::uint_fast32_t太多。@NO_NAME:是的,这是我回答中提到的4个问题中的3个。
clear; g++ test2.cpp -std=c++11 -o test2; ./test2