如何从VB.NET中的文件中读取混合字节和单精度数据? [英] How do I read mixed byte and single precision data from a file in VB.NET?

查看:105
本文介绍了如何从VB.NET中的文件中读取混合字节和单精度数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个古老的VB黑客,多年退休,最近激励编写一些代码来为游戏制作附加工具。

游戏文件包含可变数量的可变长度块,其中两个包含浮点数组。在VB6中编写应用程序来读取和修改工作得很好的文件,我想我会更新我的技能并在VB.net中重写它们,这样我就不必为我(潜在的)用户增加安装的负担。常见的对话框和其他OCX。

我花了三天时间浏览VB.net中文件处理的钝,模糊和彻头彻尾的令人困惑的文档,而没有学习如何读/写字符串,字节和从同一个文件中浮动数据。

目前我使用输入来获取一些字符串数据,在其中我获得浮点数据的指针。然后我使用SEEK和GET来读取我的浮点数组,然后再次SEEK然后PUT重写修改后的数据。

有人可以提供建议吗?



我尝试了什么:



我查看过Filestreams和My.computer.filesystem.FileGet但是找到了我的大脑通过需要以不同方式读取文件的不同位来添加。

I'm an old VB hack, many years retired, recently spurred to write some code to make add-on utilities for a game.
The game file comprises a variable number of variable length chunks, two of which contain floating point arrays. Having written apps in VB6 to read and modify the files which work pretty well, I thought I'd update my skills and rewrite them in VB.net so that I wouldn't have to burden my (potential) users with having to install the common dialog and other OCX's.
I have spent three days ploughing my way through the obtuse, obscure and downright bewildering documentation of file handling in VB.net without learning how I can read/write string, byte and float data from the same file.
At present I use input to grab some string data, within which I get pointers to the float data. I then use SEEK and GET to read my float arrays and once again SEEK and then PUT to rewrite the amended data.
Can anyone offer advice, please?

What I have tried:

I have looked at Filestreams and My.computer.filesystem.FileGet but find my brain addled by needing to read different bits of the file in different ways.

推荐答案

看看这个: ByteArrayBuilder - 用于字节的StringBuilder [ ^ ]代码在C#中,但代码转换器C#转VB和VB转C# - Telerik [ ^ ]应该如果你不能解决问题的帮助。



其实r eading和写一个满字节的文件很简单: File.ReadAllBytes(String)方法(System.IO)| Microsoft Docs [ ^ ]和 File.WriteAllBytes(String,Byte [])方法(System.IO)| Microsoft Docs [ ^ ]会为您完成。一旦你把它作为一个字节数组,就很容易处理成原生数据。
Have a look at this: ByteArrayBuilder - a StringBuilder for Bytes[^] the code is in C#, but Code Converter C# to VB and VB to C# – Telerik[^] should help if you can't work it out.

Actually reading and writing a file full of bytes is simple: File.ReadAllBytes(String) Method (System.IO) | Microsoft Docs[^] and File.WriteAllBytes(String, Byte[]) Method (System.IO) | Microsoft Docs[^] will do it for you. Once you have it as an array of bytes, it's pretty easy to process into "native data".


如果你必须尝试对代码进行行换行转换(I不推荐它!),你正在寻找 BinaryReader [ ^ ]和 BinaryWriter [ ^ ]类。它允许您对各种类型(byte,int16,int32,int64,signed byte,single,...)进行各种读写操作。



他们不喜欢提供任何功能来搜索文件。通过使用FileStream打开文件可以轻松完成,这将允许您搜索。然后可以将此FileStream传递给BinaryReader / Writer,它将允许您准确读/写所需的内容。

If you must try to do a "line for line" conversion of the code (I DONT'T recommend it!), you're looking for the BinaryReader[^] and BinaryWriter[^] classes. It lets you do all kinds of reads and writes for various types (byte, int16, int32, int64, signed byte, single, ...)

They don't provide any functionality to Seek around the file though. That's easy enough to do by opening the file with a FileStream, which will allow you to seek around. This FileStream can then be passed to a BinaryReader/Writer which will allow you to read/write exactly what you want.
Using myFile As FileStream = File.Open(filename, FileMode.Open)
    Using fileContent As New BinaryReader(myFile)
        myFile.Seek(position)
        Dim someValue As Integer = fileContent.ReadInt32();
    End Using
End Using


我在考虑你存储和检索游戏数据的要求......



我从序列化(Visual Basic)| Microsoft Docs [ ^ ],尤其是关于 BinaryFormatter类(System.Runtime.Serialization.Formatters.Binary)| Microsoft Docs [ ^ ]。这使您可以编写和读取任何数据,并避免类型转换问题。



祝你好运!
I was thinking about your requirements to store and retrieve data of game...

I'd start from Serialization (Visual Basic) | Microsoft Docs[^], especially about BinaryFormatter Class (System.Runtime.Serialization.Formatters.Binary) | Microsoft Docs[^]. This enables you to write and read any data and avoids of types converting issues.

Good luck!


这篇关于如何从VB.NET中的文件中读取混合字节和单精度数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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