什么是读取混合(即文本和二进制)数据的最佳方式? [英] What's the best way to read mixed (i.e. text and binary) data?

查看:131
本文介绍了什么是读取混合(即文本和二进制)数据的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够读取混合二进制和非二进制数据的文件格式。假设我知道输入是好的,那么最好的办法是什么?举一个例子,让我们把一个有双精度的文件作为第一行,一个换行符(0x0D 0x0A),然后是十个二进制数据字节。当然,我可以计算换行符的位置,然后制作一个BinaryReader并寻找这个位置,但是我一直在想,必须有一个更好的方法。

解决方案

您可以使用System.IO.BinaryReader。这个问题虽然是你必须在调用任何Read方法之前知道你将读取的数据类型。

<$ p $ b $ Read $ b $ Read $($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b ReadBoolean()
ReadByte()
ReadBytes(int)
ReadChar()
ReadChars()
ReadDecimal()
ReadDouble()
ReadInt16()
ReadInt32()
ReadInt64()
ReadSByte()
ReadSingle()
ReadString()
ReadUInt16()
ReadUInt32()
ReadUInt64()

当然, .IO.BinaryWriter。


I need to be able to read a file format that mixes binary and non-binary data. Assuming I know the input is good, what's the best way to do this? As an example, let's take a file that has a double as the first line, a newline (0x0D 0x0A) and then ten bytes of binary data afterward. I could, of course, calculate the position of the newline, then make a BinaryReader and seek to that position, but I keep thinking that there has to be a better way.

解决方案

You can use System.IO.BinaryReader. The problem with this though is you must know what type of data you are going to be reading before you call any of the Read methods.

Read(byte[], int, int)
Read(char[], int, int)
Read()
Read7BitEncodedInt()
ReadBoolean()
ReadByte()
ReadBytes(int)
ReadChar()
ReadChars()
ReadDecimal()
ReadDouble()
ReadInt16()
ReadInt32()
ReadInt64()
ReadSByte()
ReadSingle()
ReadString()
ReadUInt16()
ReadUInt32()
ReadUInt64()

And of course the same methods exist for writing in System.IO.BinaryWriter.

这篇关于什么是读取混合(即文本和二进制)数据的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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