Page 1 of 1

Showing the hexadecimal from an offset and endian swapping

Posted: Tue Jun 13, 2006 4:24 pm
by LuxuriousMeat
im using vb 2005 and i need to know how to show 2 bytes of the hexadecimal in a textbox and then endian swap it does anybdy nwo how to do that

Posted: Tue Jun 13, 2006 6:08 pm
by ugly-nerd

Code: Select all

    
    Dim BR As BinaryReader(New FileStream(Junk Here))
    BR.BaseStream.Position = Dec(Hex Offset Here)
    TextBox1.Text = SwapShort(BR.ReadBytes(2))
    TextBox2.Text = SwapLong(BR.ReadBytes(2))
    TextBox3.Text = SwapInteger(BR.ReadBytes(2))

    Public Function Dec(ByVal HexString As String)
    Return ("&H" & HexString)
    End Function

    Public Function SwapShort(ByVal IntIn As Short) As Short
    Dim BytesIn(1) As Byte
    Dim BytesOut(1) As Byte
    Dim X As Integer
    BytesIn = BitConverter.GetBytes(IntIn)
    For X = 0 To 1
    BytesOut(X) = BytesIn(1 - X)
    Next X
    SwapShort = BitConverter.ToInt32(BytesOut, 0)
    End Function

    Public Function SwapInteger(ByVal IntIn As Integer) As Integer
    Dim BytesIn(3) As Byte
    Dim BytesOut(3) As Byte
    Dim X As Integer
    BytesIn = BitConverter.GetBytes(IntIn)
    For X = 0 To 3
    BytesOut(X) = BytesIn(3 - X)
    Next X
    SwapInteger = BitConverter.ToInt32(BytesOut, 0)
    End Function

    Public Function SwapLong(ByVal IntIn As Integer) As Long
    Dim BytesIn(7) As Byte
    Dim BytesOut(7) As Byte
    Dim X As Integer
    BytesIn = BitConverter.GetBytes(IntIn)
    For X = 0 To 7
    BytesOut(X) = BytesIn(7 - X)
    Next X
    SwapLong = BitConverter.ToInt32(BytesOut, 0)
    End Function
Endian Swapping comes from:
http://pscode.com/vb/scripts/ShowCode.a ... &lngWId=10

Posted: Tue Jun 13, 2006 8:52 pm
by LuxuriousMeat
they all give me errors like this

Code: Select all

indexLbl.Text = SwapLong(br.ReadBytes(2))
i get this error
Value of type :I-dimensional array of Byte' cannot be converted to 'Integer'.

Posted: Wed Jun 14, 2006 6:30 am
by ugly-nerd
Im not to sure on what SwapEndian really is but I searched and someone said it was where you reverse the bytes/string. So here is what I came up with:

Code: Select all

    Public Function SwapEndian(ByVal Input As String)
        Dim Char1 As String = Nothing
        Dim Str1 As String = Nothing
        For i As Short = Len(Input) To 1 Step -1
            Char1 = Mid(Input, i, 1)
            Str1 &= Char1
        Next
        Return Str1
    End Function
If it doesnt work, try changing Input, Char1, Str1 as integers.

Good luck!

Posted: Wed Jun 14, 2006 10:47 am
by LuxuriousMeat
endian swapping is like this if you have 8AE4 FFE4 when you endian swap them you get E4FF E48A

Posted: Wed Jun 14, 2006 10:59 am
by ugly-nerd
Ok, I have no damn clue on how to do that. If you find a function for it, would you please PM/AIM(li hyper il) it to me? The last function I posted wont work. The other ones dont work because it requires a certain number of bytes to endian swap.

Posted: Wed Jun 14, 2006 11:12 am
by TfAv1228

Code: Select all

Public Function SwapEndian(Hexstr As String) As String
    Dim Tmp As String
    For i As Integer = 0 To (HexStr.Length/2)-1
       Tmp = HexStr.Substring(i*2,2) & Tmp
    Next
    Return Tmp
End Function
Public Function DecToHex(DecVal As Long) As String
    Return DecVal.ToString("X")
End Function
Public Function HexToDex(HexStr As String) As Long
    Return Convert.ToInt64(HexStr,16)
End Function

There damn