Showing the hexadecimal from an offset and endian swapping

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Showing the hexadecimal from an offset and endian swapping

Post 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
Image
ugly-nerd





Posts: 23
Joined: Sat May 21, 2005 6:51 am

Post 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
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Post 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'.
Image
ugly-nerd





Posts: 23
Joined: Sat May 21, 2005 6:51 am

Post 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!
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Post by LuxuriousMeat »

endian swapping is like this if you have 8AE4 FFE4 when you endian swap them you get E4FF E48A
Image
ugly-nerd





Posts: 23
Joined: Sat May 21, 2005 6:51 am

Post 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.
TfAv1228




Miner Logistician Droplet

Posts: 276
Joined: Sun Sep 05, 2004 4:40 pm

Post 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
Sig breaks rules, read the rules before reposting.
Post Reply