处理字节序和文件的最常用方法C ++ [英] most common way to deal with endianness and files C++

查看:102
本文介绍了处理字节序和文件的最常用方法C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用char读取/写入8位整数到文件中。不久之后,我意识到我需要能够使用超过256个可能的值。我做了一些关于如何将16位整数读/写到文件的研究,并且意识到了大小端的概念。我进行了更多研究,发现了几种处理字节序的方法,并且我还学习了一些编写与字节序无关的代码的方法。我的总体结论是,我必须首先检查正在使用的系统使用的是大端还是小端,然后根据系统使用的类型来更改字节序,然后使用这些值。

I started out just reading/writing 8-bit integers to files using chars. It was not very long before I realized that I needed to be able to work with more than just 256 possible values. I did some research on how to read/write 16-bit integers to files and became aware of the concept of big and little endian. I did even more research and found a few different ways to deal with endianness and I also learned some ways to write endianness-independent code. My overall conclusion was that I have to first check if the system I am using is using big or little endian, change the endianness depending on what type the system is using, and then work with the values.

我无法找到的一件事是在C ++中读取/写入文件时(不联网)的最佳/最常见处理字节序的方法。那么我应该怎么做呢?为澄清起见,我要求一种在大型和小型字节序系统之间读取/写入16/32位整数的最佳方法。因为我担心不同系统之间的字节序关系,所以我也想要一个跨平台的解决方案。

The one thing I have not been able to find is the best/most common way to deal with endianness when reading/writing to files in C++ (no networking). So how should I go about doing this? To help clarify, I am asking for the best way to read/write 16/32-bit integers to files between big and little endian systems. Because I am concerned about the endianness between different systems, I would also like a cross-platform solution.

推荐答案

最常见的方法只是将您的内存中值在写入文件之前通过htons()或htonl()传递,并且在从文件读回之后将读取的数据通过ntohs()或ntohl()传递。 (htons()/ ntohs()处理16位值,htonl()/ ntohl()处理32位值)

The most common way is simply to pass your in-memory values through htons() or htonl() before writing them to the file, and also pass the read data through ntohs() or ntohl() after reading it back from the file. (htons()/ntohs() handle 16-bit values, htonl()/ntohl() handle 32-bit values)

为大端CPU编译时,这些函数是no-ops(它们只是逐字返回您传递给它们的值),因此这些值将以big-endian格式写入文件中。当为低端字节序的CPU编译时,这些函数将对传入的值进行字节序交换并返回交换的版本,因此,这些值将再次以大端字节序格式写入文件。

When compiled for a big-endian CPU, these functions are no-ops (they just return the value you passed in to them verbatim), so the values will get written to the file in big-endian format. When compiled for a little-endian CPU, these functions endian-swap the passed-in value and return the swapped version, so again the values will get written to the file in big-endian format.

这样,文件中的值始终以big-endian格式存储,并且在将它们传输到内存或从内存传输时,总是将其转换为合适的(CPU本地)格式。这是最简单的方法(因为您不必编写或调试任何条件逻辑),也是最常见的方法(这些功能已在几乎所有平台上实现并可用)

That way the values in the file are always stored in big-endian format, and they always get converted to/from the appropriate (CPU-native) format when being transferred to/from memory. This is the simplest way to do it (since you don't have to write or debug any conditional logic), and the most common (these functions are implemented and available on just about all platforms)

这篇关于处理字节序和文件的最常用方法C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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