在 C 中解析二进制数据? [英] Parsing Binary Data in C?
问题描述
是否有任何关于如何在 C 中读取和解析二进制数据的库或指南?
Are there any libraries or guides for how to read and parse binary data in C?
我正在研究一些功能,它将在网络套接字上接收 TCP 数据包,然后根据规范解析二进制数据,通过代码将信息转换为更可用的形式.
I am looking at some functionality that will receive TCP packets on a network socket and then parse that binary data according to a specification, turning the information into a more useable form by the code.
是否有任何库可以做到这一点,或者甚至是执行此类事情的入门书?
Are there any libraries out there that do this, or even a primer on performing this type of thing?
推荐答案
在 C/C++ 中执行此操作的标准方法实际上是按照gwaredd"的建议强制转换为结构
The standard way to do this in C/C++ is really casting to structs as 'gwaredd' suggested
它并不像人们想象的那样不安全.您首先转换为您期望的结构,如在他/她的示例中,然后您测试该结构的有效性.您必须测试最大值/最小值、终止序列等.
It is not as unsafe as one would think. You first cast to the struct that you expected, as in his/her example, then you test that struct for validity. You have to test for max/min values, termination sequences, etc.
无论您使用什么平台,都必须阅读 Unix 网络编程,第 1 卷:套接字网络 API.买、借、偷(受害者会明白,这就像偷食物什么的……),但一定要读.
What ever platform you are on you must read Unix Network Programming, Volume 1: The Sockets Networking API. Buy it, borrow it, steal it ( the victim will understand, it's like stealing food or something... ), but do read it.
阅读史蒂文斯之后,大部分内容会更有意义.
After reading the Stevens, most of this will make a lot more sense.
这篇关于在 C 中解析二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!