如何在vb.net中的字节数组中读取特定字节?

发布于 2025-01-21 18:37:54 字数 506 浏览 0 评论 0原文

我使用dim settingsbinary as byte()= my.computer.filesystem.readallbytes(settingsfile)使用dim settingsbinary aste,其中settingsfile是我二进制文件的路径。

假设我的二进制文件有三个字节,我想将第一个字节读为布尔值(00 = false,01 = true)。我该如何获得这些字节?我尝试使用这个问题的答案阅读这三个字节,但我无法将其缠绕在上面。

只是为了澄清,我需要单独获取三个字节:获取第一个字节,然后将checkbox1.Checked设置为第一个字节,依此类推,依此类推。

I have a binary file being loaded into a Byte array using Dim settingsBinary As Byte() = My.Computer.FileSystem.ReadAllBytes(settingsFile) where settingsFile is the path to my binary file.

Let's say that my binary file has got three bytes and I want to read the first byte as a Boolean (00 = False, 01 = True). How can I get those bytes? I have tried to use this question's answer to read those three bytes, but I can't wrap my head around it.

Just to clarify, I need to get the three bytes separately: Get the first byte, then set CheckBox1.Checked to the first byte, and so on with the other bytes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-01-28 18:37:55

字节数组就像其他任何数组一样工作:您可以使用 indexer 访问特定元素。

Dim firstByte As Byte = settingsBinary(0)
Dim secondByte As Byte = settingsBinary(1)
Dim thirdByte As Byte = settingsBinary(2)

然后,您可以将字节转换为布尔值:(

Dim firstBoolean As Boolean
Select Case firstByte
    Case 0
        firstBoolean = False
    Case 1
        firstBoolean = True
    Case Else
        Throw New Exception("Invalid first byte in settings file: " & firstByte)
End Select

将转换逻辑概括为可用于所有三个字节的方法,将其作为练习给读者。)

A byte array works just like any other array: You can use an indexer to access a specific element.

Dim firstByte As Byte = settingsBinary(0)
Dim secondByte As Byte = settingsBinary(1)
Dim thirdByte As Byte = settingsBinary(2)

And then you can convert the byte into a boolean:

Dim firstBoolean As Boolean
Select Case firstByte
    Case 0
        firstBoolean = False
    Case 1
        firstBoolean = True
    Case Else
        Throw New Exception("Invalid first byte in settings file: " & firstByte)
End Select

(Generalizing the conversion logic into a method that can be used for all three bytes is left as an exercise to the reader.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文