Hex Editing Functions

Discussion in 'Programming General' started by Darthatron, Feb 15, 2008.

Hex Editing Functions
  1. Unread #1 - Feb 15, 2008 at 7:06 AM
  2. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Hex Editing Functions

    Ok, here are some pretty basic Hex Editing Functions for... Well... Hex Editing. :p I have no idea why anyone here would want to use these functions, except maybe save file editing... Or something.

    This is a Function thats get a string of Bytes...
    Code:
    Public Function ReadHEX(FilePath As String, Start As Long, Length As Long) As String
    On Error Resume Next
    Dim iFile As Long
    Dim bytData As Byte
    Dim sHex As String
    Dim i As Long
        Start = Start + 1
        iFile = FreeFile
        sHex = ""
        i = 0
        Open FilePath For Binary As iFile
            For i = Start To (Start + Length - 1)
                Get iFile, i, bytData
                sHex = sHex & Right("00" & Hex(bytData), 2)
            Next i
        Close iFile
    ReadHEX = sHex
    End Function
    Example of use:
    Code:
    strString = ReadHEX("C:\File.bin", 20542, 1)
    ________________________________________________

    This next Function lets you write a single Byte into a file. If you could be bothered you could easily use it to make a 'WriteHEX' Function, but I am far too lazy and have no need for it.
    Code:
    Public Function Write1Byte(FilePath As String, Start As Long, Data As String)
    On Error Resume Next
    Dim iFile As Long
    Dim bytData As Byte
        Start = Start + 1
        iFile = FreeFile
        Open FilePath For Binary As iFile
            bytData = CInt("&H" & Right("00" & Data, 2))
        Put iFile, Start, bytData
        Close iFile
    End Function
    Example of use:
    Code:
    Write1Byte "C:\File.bin",, 20542, "FF"
    ________________________________________________

    The following code flips a HEX string around. Sometimes in Binary Data to 'goto' a location you would put the actual location of the String, reversed. So... For the location &H123456 to 'goto' the location would be &H563412.
    Code:
    Public Function ReverseHEX(HEXData As String) As String
    Dim iNum As Long
    Dim HEXHolder As String
        If (Len(HEXData)) <> Int((Len(HEXData)) / 2) * 2 Then HEXData = "0" & HEXData
        
        For iNum = 0 To Len(HEXData) + 1
            If Len(HEXData) <= 1 Then GoTo EndNow
            HEXHolder = HEXHolder & Right(HEXData, 2)
            HEXData = Left(HEXData, Len(HEXData) - 2)
        Next iNum
    EndNow:
    ReverseHEX = HEXHolder
    End Function
    Example of use:
    Code:
    strString = ReverseHEX("123456")
    Hell... that's it, I'm too lazy to elaborate anymore. :p Have fun Hex Editing! :D

    EDIT: Obviously nobody used these yet, because I used a function that I didn't include... Anyway, fixed it.
     
  3. Unread #2 - Feb 27, 2008 at 9:38 PM
  4. colinrusovic
    Joined:
    Dec 8, 2007
    Posts:
    79
    Referrals:
    0
    Sythe Gold:
    0

    colinrusovic Member

    Hex Editing Functions

    not bad.....well im not rlly sure if it is or not cuz i don;t know any of this real well, but it looked like it took a while :)
     
  5. Unread #3 - Feb 28, 2008 at 10:15 PM
  6. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    Hex Editing Functions

    No... they took about 5 minutes each to write, they are pretty basic. :D The hard/annoying part is finding the offsets you want to edit and what they do. :( Thanks for commenting.
     
< [SOURCE] Basic C++ Hangman in the Command Prompt | Get the Error-license information for this component not found? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site