vbdotnetcommon
These are common chunks of code I use in VB.Net to read binary streams into variables.
Enjoy!
Public Function ReadUInt16_LE(Arr() As Byte, Offset As Int32) As UInt16 Dim V As UInt16 = 0 V = CByte(Arr(Offset + 0)) V = V Or (CUShort(Arr(Offset + 1)) << 8) Return V End Function Public Function ReadInt16_LE(Arr() As Byte, Offset As Int32) As Int16 Dim V As Int16 = 0 V = CByte(Arr(Offset + 0)) V = V Or (CShort(Arr(Offset + 1)) << 8) Return V End Function Public Function ReadInt32_LE(Arr() As Byte, Offset As Int32) As Int32 Dim V As Int32 = 0 V = CByte(Arr(Offset + 0)) V = V Or (CInt(Arr(Offset + 1)) << 8) V = V Or (CInt(Arr(Offset + 2)) << 16) V = V Or (CInt(Arr(Offset + 3)) << 24) Return V End Function Public Function ReadUInt32_LE(Arr() As Byte, Offset As Int32) As UInt32 Dim V As UInt32 = 0 V = CByte(Arr(Offset + 0)) V = V Or (CUInt(Arr(Offset + 1)) << 8) V = V Or (CUInt(Arr(Offset + 2)) << 16) V = V Or (CUInt(Arr(Offset + 3)) << 24) Return V End Function Public Sub WriteUInt16_BE(Arr() As Byte, Offset As Int32, Value As UInt16) Arr(Offset + 1) = CByte(Value And 255US) Value = Value >> 8 Arr(Offset) = CByte(Value And 255US) End Sub Public Sub WriteUInt16_LE(Arr() As Byte, Offset As Int32, Value As UInt16) Arr(Offset) = CByte(Value And 255US) Value = Value >> 8 Arr(Offset + 1) = CByte(Value And 255US) End Sub Public Sub WriteUInt32_BE(Arr() As Byte, Offset As Int32, Value As UInt32) For I As Integer = 3 To 0 Step -1 Arr(Offset + I) = CByte(Value And 255) Value = Value >> 8 Next End Sub Public Sub WriteUInt32_LE(Arr() As Byte, Offset As Int32, Value As UInt32) For I As Integer = 0 To 3 Arr(Offset + I) = CByte(Value And 255) Value = Value >> 8 Next End Sub Friend Function ReadAsciiZ(Arr() As Byte, Offset As Int32, Count As Int32) As String Dim RetStr As New System.Text.StringBuilder(Count) Dim Pos As Int32 = 0 Dim B As Byte While Pos < Count B = Arr(Offset + Pos) If B = 0 Then Exit While RetStr.Append(Chr(B)) Pos += 1 End While 'If Pos < Count Then RetStr = RetStr.Substring(0, Pos) Return RetStr.ToString End Function While True If Console.KeyAvailable Then Select Case Console.ReadKey.KeyChar Case "q"c : Exit While End Select End If ' Do stuff?? System.Threading.Thread.Sleep(100) End While Public Function StreamCopyToEnd(ByVal StrmTo As System.IO.Stream, ByVal StrmFrom As System.IO.Stream) As Int64 ' Assumes streams already in their proper spots, and length is ok Const BufferSize As Int32 = 1024 * 64 ' 64K Dim Buffer(0 To (BufferSize - 1)) As Byte ' Dim BytesCopied As Int64 = 0 Dim BytesRead As Int32 = 1 ' While BytesRead > 0 BytesRead = StrmFrom.Read(Buffer, 0, BufferSize) StrmTo.Write(Buffer, 0, BytesRead) BytesCopied += BytesRead End While ' Return BytesCopied End Function
Signed / Unsigned conversions
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Structure UInt8_Union <System.Runtime.InteropServices.FieldOffset(0)> Public I As SByte <System.Runtime.InteropServices.FieldOffset(0)> Public U As Byte End Structure <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Structure UInt16_Union <System.Runtime.InteropServices.FieldOffset(0)> Public I As Int16 <System.Runtime.InteropServices.FieldOffset(0)> Public U As UInt16 End Structure <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Structure UInt32_Union <System.Runtime.InteropServices.FieldOffset(0)> Public I As Int32 <System.Runtime.InteropServices.FieldOffset(0)> Public U As UInt32 End Structure <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Structure UInt64_Union <System.Runtime.InteropServices.FieldOffset(0)> Public I As Int64 <System.Runtime.InteropServices.FieldOffset(0)> Public U As UInt64 End Structure
vbdotnetcommon.txt · Last modified: 2023/05/16 19:48 by srbios