简体   繁体   English

如何将python float转换为要在C ++程序中解释的字节

[英]how to convert python float to bytes to be interpreted in C++ programm

I need to build a python script that export data to a custom file format. 我需要构建一个将数据导出为自定义文件格式的python脚本。 This file is then read by a cpp programm (I have the source code, but cannot compile it). 然后,该文件由cpp程序读取(我有源代码,但无法编译)。

custome file fomat spec: 定制文件格式规格:

  • The file must be little endian. 该文件必须为小端。
  • The float must be 4 bytes long. 浮点数必须为4个字节长。

I'm failing at exporting python float to bytes. 我无法将python float导出为字节。 thus crashing the cpp app without any error trace. 因此崩溃了cpp应用程序而没有任何错误跟踪。 If i try to fill all the float with 0 it load perfectly fine, but if i try anything else it crashes. 如果我尝试用0填充所有浮点数,则加载效果会很好,但是如果我尝试其他任何操作,它将崩溃。

This is how the cpp app read the float 这就是cpp应用读取浮动信息的方式

double Eds2ImporterFromMemory::read4BytesAsFloat(long offset) const
{
    // Read data.
    float i = 0;
    memcpy(&i, &_data[offset], 4);

    return i;
}

And I try to export the python float as follows: 我尝试如下导出python float:

def write_float(self, num):
    # pack float 
    self.f.write(struct.pack('<f', float(num)))

And also like this as some people suggested me: 也有人这样建议我:

def write_float(self, num):
    # unpack as integer the previously pack as float number    
    float_hex = struct.unpack('<I', struct.pack('<f', num))[0]
    self.f.write(float_hex.to_bytes(4, byteorder='little'))

But it fails every time. 但是它每次都会失败。 I'm not a cpp guy as you can see. 如您所见,我不是cpp家伙。 Do you have an idea on why my python script is not working. 您对我的python脚本为什么不起作用有任何想法。

Thanks in advance 提前致谢

Please excuse me for being really bad at python, but i tried to produce your desired result and it works fine for me. 请原谅我对python非常不好,但是我试图产生您想要的结果,它对我来说很好。

The python code: python代码:

import struct

value = 13.37

ba = bytearray(struct.pack("f", value))  
for b in ba:
    print("%02x" % b)

I imagine if you would just concat that (basically write the hexadecimal representation to a file in that order), it would work just fine. 我想如果您只是希望做到这一点(基本上按该顺序将十六进制表示形式写入文件),那么它将很好地工作。

Either way, it will output 无论哪种方式,它都会输出

85
eb
55
41

Which i put in an array of unsigned chars and used memcpy in the same way your C++ code did: 我将其放入无符号字符数组并以与您的C ++代码相同的方式使用memcpy:

unsigned char data[] = {0x85, 0xeb, 0x55, 0x41};

float f;
memcpy(&f, data, sizeof(float));

std::cout << f << std::endl;
std::cin.get();

Which will output 13.37 as you would expect. 如您所料,它将输出13.37 If you cannot reproduce the correct result this way, i suspect that the error occurs somewhere else, in which case it would be helpful to see how the format is written by python and how it's read by C++. 如果您不能重现正确的结果这样一来,我怀疑是别的地方发生了错误,在这种情况下,这将是有益的,看看如何格式由Python编写的,它是如何通过C ++读取。

Also, keep in mind that there are several ways to represent the byte array, like: 另外,请记住,有几种方法可以表示字节数组,例如:

\\x85eb5541 , 0x85, 0xeb, 0x55, 0x41 , 85eb5541 and so on. \\x85eb55410x85, 0xeb, 0x55, 0x4185eb5541等。 To me it seems very likely that you just aren't outputting the correct format and thus the "parser" crashes. 对我来说,很可能您只是没有输出正确的格式,因此“解析器”崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM