C ++ - 以小端序列化双向二进制文件 [英] C++ - serialize double to binary file in little endian

查看:172
本文介绍了C ++ - 以小端序列化双向二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个以小字节顺序将 double 写入二进制文件的函数。
到目前为止,我有类 BinaryWriter 实现:

  void BinaryWriter :: open_file_stream(const String& path) 
{
//打开输出流
m_fstream.open(path.c_str(),std :: ios_base :: out | std :: ios_base :: binary);
m_fstream.imbue(std :: locale :: classic());
}

void BinaryWriter :: write(int v)
{
char data [4];
data [0] = static_cast< char>(v& 0xFF);
data [1] = static_cast< char>((v>>> 8)& 0xFF);
data [2] = static_cast< char>((v>>> 16)& 0xFF);
data [3] = static_cast< char>((v>> 24)& 0xFF);
m_fstream.write(data,4);
}

void BinaryWriter :: write(double v)
{
// TBD
}

void BinaryWriter :: write(int v)已使用 将六进制数据输出到正确方法的答案一个文件? post。

不确定如何实现 void BinaryWriter :: write(double v)

我尝试天真跟随 void BinaryWriter :: write(int v)实现,但它没有工作。我想我完全不了解这个实现。



谢谢你们

解决方案你没有写这个,但我假设你运行的机器是BIG endian,否则写一个double与编写一个int一样,只有8个字节。

  const int __one__ = 1; 
const bool isCpuLittleEndian = 1 == *(char *)(& __ one__); // CPU endianness
const bool isFileLittleEndian = false; //输出endianness - 你选择:)


void BinaryWriter :: write(double v)
{
if(isCpuLittleEndian ^ isFileLittleEndian){
char数据[8],* pDouble =(char *)(double *)(& v); (int i = 0; i< 8; ++ i)
{
data [i] = pDouble [7-i];
}
m_fstream.write(data,8);
}
else
m_fstream.write((char *)(& v),8);
}


I'm trying to implement a function that writes double to binary file in little endian byte order.
So far I have class BinaryWriter implementation:

void BinaryWriter::open_file_stream( const String& path )
{
 // open output stream
  m_fstream.open( path.c_str(), std::ios_base::out | std::ios_base::binary);
  m_fstream.imbue(std::locale::classic());   
}

void BinaryWriter::write( int v )
{
  char data[4];
  data[0] = static_cast<char>(v & 0xFF);
  data[1] = static_cast<char>((v >> 8) & 0xFF);
  data[2] = static_cast<char>((v >> 16) & 0xFF);
  data[3] = static_cast<char>((v >> 24) & 0xFF);
  m_fstream.write(data, 4);
}

void BinaryWriter::write( double v )
{
   // TBD
}

void BinaryWriter::write( int v ) was implemented using Sven answer to What is the correct way to output hex data to a file? post.
Not sure how to implement void BinaryWriter::write( double v ).
I tried naively follow void BinaryWriter::write( int v ) implementation but it didn't work. I guess I don't fully understand the implementation.

Thank you guys

解决方案

You didn't write this, but I'm assuming the machine you're running on is BIG endian, otherwise writing a double is the same as writing an int, only it's 8 bytes.

const int __one__ = 1;
const bool isCpuLittleEndian = 1 == *(char*)(&__one__); // CPU endianness
const bool isFileLittleEndian = false;  // output endianness - you choose :)


void BinaryWriter::write( double v )
{
  if (isCpuLittleEndian ^ isFileLittleEndian) {
    char data[8], *pDouble = (char*)(double*)(&v);
    for (int i = 0; i < 8; ++i) {
      data[i] = pDouble[7-i];
    }
    m_fstream.write(data, 8);
  }
  else
    m_fstream.write((char*)(&v), 8);
}

这篇关于C ++ - 以小端序列化双向二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆