Well, you are asking a question in the form, "I need a solution to this specific problem." I could answer it directly, but it wouldn't provide any insight into the underlying problem and the next time you face something similar you'd have another question.
If we change your specific question into a general question, we go from, "How do I display the numeric values of two bytes at this location?" to, "How do you read a byte at a specific offset in a file?" I'm going to walk you through the train of thought that leads to the solution to this general problem.
The basis of I/O operations in .NET is the
Stream class. It defines a
Position property that indicates the current location in the stream and a
Seek method that allows you to move to a specific location in a stream. Not all streams allow random access (seeking), so these will work only if the
CanSeek property returns true. I'll cover two approaches, but there's no reason why seeking shouldn't be supported in the classes we're using.
Stream is abstract (MustInherit), so we need to find an implementation that does the job.
FileStream looks like a good choice, since we're working with a file. Reading the documentation indicates that in most cases, we'll be able to use random access, though if the file isn't a disk file this is not true, so we will need to check the
CanSeek property before doing any random access.
Now that we know what our tool will be, let's discuss the requirements of our function. Our desired output is the value of a byte at a particular offset. We definitely need to pass the offset into this function, but it needs to have access to the stream as well. We could pass it a file name, but then retrieving multiple bytes in a row would open the file multiple times and seek multiple times; it's probably better to take a
Stream parameter. Since we will be moving around in the stream, we'll make that parameter
ByRef to point out that it will be modified by calling our function. We'll return
Integer instead of a
Byte; if the function fails to read the value (for example, the stream is smaller than the offset indicates), we'll return the integer value -1, which is impossible to represent in a Byte so the caller can tell if an error happened. Finally, we want to support the operation whether random access is available or not.
Given these requirements, the function is pretty straightforward:
Code:
' NOTE: to maintain consistency between streams that do and don't support random access,
' the offset is always relative to the current position. If you want the offset to be from
' the beginning, make sure the stream position is at the beginning.
Public Function GetByteAtOffset(ByRef stream As Stream, ByVal offset As Long) As Integer
' Initialize to invalid value; nothing changes it if errors happen
Dim byteValue As Integer = -1
Dim seekResult As Long
If stream.CanSeek Then
seekResult = stream.Seek(offset, SeekOrigin.Current)
If seekResult = offset Then
' This executes if the seek call succeeds; ReadByte returns -1 if it can't read
' the value, so this conveniently handles error cases.
byteValue = stream.ReadByte()
End If
Else
' We can't seek; we'll have to read EVERY value until we reach offset
Dim byteCount As Integer = 0
Dim lastValue As Integer = 0
While byteCount < offset AndAlso Not lastValue = -1
lastValue = stream.ReadByte()
byteCount += 1
End While
' We've advanced the file position to the offset; set the final value
byteValue = stream.ReadByte()
End If
Return byteValue
End Function
This can be used to solve your problem easily; you first tell it to read to offset 8800 (I'm assuming it's in hex), then read the byte, then read again since the function advances the pointer:
Code:
Dim offsetAddress As Long = &H8800
Dim values(1) As Integer
Using input As New FileStream("data.iso", FileMode.Open)
values(0) = GetByteAtOffset(input, 10)
values(1) = input.ReadByte()
End Using
If values(0) = -1 OrElse values(1) = -1 Then
Console.WriteLine("Error reading file.")
Else
Console.WriteLine("Values: {0:X2} {1:X2}", values(0), values(1))
End If