Question String to Hex?

Mikeeeyy

New member
Joined
Jun 12, 2013
Messages
1
Programming Experience
1-3
What I'm doing is decompiling Call of Duty Fastfiles, now everything is okay but I'm struggling to retrieve the size of the files within the fastfile.
For example:
3e7Ka.png
Incase you didn't know this program is HxD, but obviously I can't use this in VB.NET :/
The part that is highlighted is the size of the file, the file name is maps/_debug.gsc. Now I can retrieve the '??..' and put it into its own array of bytes.
I know how to convert this with the windows calculator, put it into programmer then paste in the highlighted hex on the left into hex and then change it to Dec then it gives you the size. I just don't know how to do this in VB.NET :/ As you can see on the left, the .. on the part on the right, one of them does actually have a value, '01'. HxD just displays them as a .
So I basically need to retrieve the value of each highlighted character on the right then convert that into hex?

Please help, I've been at this for ages now!
 
You really don't have to convert anything, you just load up the file in a byte array or collection and work with it there. First you need to find the area you want to patch. The cleanest way to handle that is probably through a custom FastFile class. You can use http://wiki.modsrepository.com/index.php/Call_of_Duty_4:_FastFile_Format to map the file.

In any case, nowhere in there do you need to use strings, unless you want to display or log some of it.
 
Note: If you ever need to convert a Hex string to a decimal number, the code is:

Dim stx As String, inum As Integer
stx = "F5" 'or any hex number up to 7 characters
inum = Convert.ToInt32(stx, 16)

Of course, you will need to validate the string input first, to make sure it's a legitimate Hex value with no spaces or invalid characters.
 
Of course, you will need to validate the string input first, to make sure it's a legitimate Hex value with no spaces or invalid characters.
You can use the Integer.TryParse method to do both in one go. If you use the overload with four parameters and specify NumberStyles.AllowHexSpecifier or NumberStyles.HexNumber then it will validate and convert hexadecimal strings.
 
TryParse Hex

That's very interesting. But exactly how do you do this in code? Here is what I tried, but couldn't quite get it. What value is assigned to provider?

VB.NET:
Module Module1

    Sub Main()
        Dim strval As String
        Dim style As Globalization.NumberStyles   'HexNumber or AllowHexSpecifier
        Dim provider As IFormatProvider
        Dim result As Integer
        Dim returnValue As Boolean
        style = Globalization.NumberStyles.HexNumber  'or AllowHexSpecifier 
        Console.Write("Enter a hex value:  ")
        strval = Console.ReadLine()
        returnValue = Integer.TryParse(strval, style, provider, result)
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub

End Module
 
Last edited:
That's very interesting. But exactly how do you do this in code? Here is what I tried, but couldn't quite get it. What value is assigned to provider?

VB.NET:
Module Module1

    Sub Main()
        Dim strval As String
        Dim style As Globalization.NumberStyles   'HexNumber or AllowHexSpecifier
        Dim provider As IFormatProvider
        Dim result As Integer
        Dim returnValue As Boolean
        style = Globalization.NumberStyles.HexNumber  'or AllowHexSpecifier 
        Console.Write("Enter a hex value:  ")
        strval = Console.ReadLine()
        returnValue = Integer.TryParse(strval, style, provider, result)
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub

End Module
If you want to use the current culture, as you normally would, then you don't provide anything to the 'provider' parameter:
Dim inputs As String() = New String() {"1A2B3C", "Hello World"}
Dim output As Integer

For Each input As String In inputs
    If Integer.TryParse(input, NumberStyles.HexNumber, Nothing, output) Then
        MessageBox.Show(String.Format("{0} in hexadecimal equates to {1} in decimal.", input, output))
    Else
        MessageBox.Show(String.Format("'{0}' is not a valid 32-bit hexadecimal number.", input))
    End If
Next
 
Thank you. "Nothing" worked.

VB.NET:
    Sub Main()
        Dim strval As String, result As Integer
        Dim style As Globalization.NumberStyles
        style = Globalization.NumberStyles.HexNumber  'or AllowHexSpecifier  
        Console.Write("Enter a hex value:  ")
        strval = Console.ReadLine()
        Integer.TryParse(strval, style, Nothing, result)
        Console.WriteLine(result)   'returns 0 if conversion failed
        Console.ReadLine()
        End Sub

The following code also works:

VB.NET:
     Sub Main()
        Dim strval As String, result As Integer
        Dim style As Globalization.NumberStyles
        Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.CurrentCulture
        style = Globalization.NumberStyles.HexNumber  'or AllowHexSpecifier  
        Console.Write("Enter a hex value:  ")
        strval = Console.ReadLine()
        Integer.TryParse(strval, style, provider, result)    'returns 0 if conversion fails
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub
 
Last edited:
Back
Top