我有一個二進制檔案被加載到一個位元組陣列中Dim settingsBinary As Byte() = My.Computer.FileSystem.ReadAllBytes(settingsFile),其中 settingsFile 是我的二進制檔案的路徑。
假設我的二進制檔案有三個位元組,我想將第一個位元組讀取為布林值(00 = False,01 = True)。我怎樣才能得到這些位元組?我試圖使用這個問題的答案來讀取這三個位元組,但我無法理解它。
為了澄清,我需要分別獲取三個位元組:獲取第一個位元組,然后設定CheckBox1.Checked為第一個位元組,以此類推其他位元組。
uj5u.com熱心網友回復:
位元組陣列的作業方式與任何其他陣列一樣:您可以使用索引器訪問特定元素。
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
(將轉換邏輯概括為可用于所有三個位元組的方法,留給讀者作為練習。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458906.html
