简体   繁体   English

在C#.NET中读取自定义二进制数据格式

[英]Reading custom binary data formats in C# .NET

I'm trying to write a simple reader for AutoCAD's DWG files in .NET. 我正在尝试为.NET中的AutoCAD DWG文件编写一个简单的阅读器。 I don't actually need to access all data in the file so the complexity that would otherwise be involved in writing a reader/writer for the whole file format is not an issue. 我实际上并不需要访问文件中的所有数据,因此,为整个文件格式编写读写器所涉及的复杂性不是问题。

I've managed to read in the basics, such as the version, all the header data, the section locator records, but am having problems with reading the actual sections. 我设法阅读了基础知识,例如版本,所有标题数据,节定位器记录,但是在阅读实际节时遇到了问题。

The problem seems to stem from the fact that the format uses a custom method of storing some data types. 问题似乎源于以下事实:该格式使用一种自定义方法来存储某些数据类型。 I'm going by the specs here: 我要按照这里的规格:

http://www.opendesign.com/files/guestdownloads/OpenDesign_Specification_for_.dwg_files.pdf http://www.opendesign.com/files/guestdownloads/OpenDesign_Specification_for_.dwg_files.pdf

Specifically, the types that depend on reading in of individual bits are the types I'm struggling to read. 具体来说,依赖于各个位读入的类型就是我正在努力读取的类型。 A large part of the problem seems to be that C#'s BinaryReader only lets you read in whole bytes at a time, when in fact I believe I need the ability to read in individual bits and not simply 8 bits or a multiple of at a time. 问题的很大一部分似乎是C#的BinaryReader一次只能读取整个字节,而实际上我认为我需要读取单个位的能力,而不是一次读取8位或整数倍的能力。

It could be that I'm misunderstanding the spec and how to interpret it, but if anyone could clarify how I might go about reading in individual bits from a stream, or even how to read in some of the variables types in the above spec that require more complex manipulation of bits than simply reading in full bytes then that'd be excellent. 可能是我对规范及其理解有误解,但如果有人可以澄清我如何去读取流中的单个位,甚至如何读取上述规范中的某些变量类型,需要比仅读取完整字节更复杂的位操作,那就太好了。

I do realise there are commercial libraries out there for this, but the price is simply too high on all of them to be justifiable for the task at hand. 我确实知道那里有商业图书馆,但是它们的价格实在太高了,不足以应付当前的任务。

Any help much appreciated. 任何帮助,不胜感激。

You can always use BitArray class to do bit wise manipulation. 您始终可以使用BitArray类进行按位操作。 So you read bytes from file and load them into BitArray and then access individual bits. 因此,您从文件中读取字节并将其加载到BitArray中,然后访问各个位。

For the price of any of those libraries you definitely cannot develop something stable yourself. 对于任何这些库的价格,您绝对不能自己开发稳定的产品。 How much time did you spend so far? 到目前为止,您花了多少时间?

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

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