I am very much a beginner to VB.net and I'm still flicking through tutorials, learning basic stuff however I need to make a program fast and need help with it. I need to know how to make the form open a game save, whether that be for PC, Xbox or anything else. Any help would be muchly appreciated, thanks
What exactly do you mean by "a game save" and what exactly do you mean by "open"? Do you mean run the game and load previously saved progress? You may think that it's obvious but it's not, so you have to provide a FULL and CLEAR explanation.
I am trying to make a mini hex edit program. So I can load an Xbox game save into it and then the program will replace all 'x' with 'y'. (Like the replace feature on hex edit programs). I believe I need X360.dll to load the save into the form for editing but I'm very confused.
That's a far better description of what you want to do. That's the sort of information you need to provide up front because, if we have to guess, there's every chance that we will guess wrong.
You don't need any DLL to open a binary file. All a hex editor does is read the raw bytes of a file and display them as a series of hexadecimal numbers. Each byte can be displayed as a pair of hexadecimal digits.
So, the first job is to read the data from the file. That's a one-liner:
Dim bytes As Byte() = IO.File.ReadAllBytes("file path here")
The next step is to convert each of those Bytes to a hexadecimal representation of the number. This is made easier by the Array.ConvertAll method, which will take an array as input and a function for converting one element and then spit out another array where each element is the result of calling that function and passing in the corresponding element from the original array. For instance, if you have the array {1, 2, 3} and you call Array.ConvertAll with a function that multiplies by 10, you will get back the array {10, 20, 30}. In this case, we want a function that will convert a Byte to a String containing a 2-digit hexadecimal number:
Dim hex As String() = Array.ConvertAll(bytes, Function(b) Convert.ToString(b, 16).PadLeft(2, "0"c))
ConvertAll takes two arguments: the first is the array, in this case 'bytes' and the second is a function for converting an element. In this case, that function takes a Byte as input, converts it to a String in base-16 format and then pads it to two characters with zeroes if necessary. That code may be a little confusing for those not used to lambda expressions so it may be more clear to write a conventional method and then use that:
Private Function ConvertByteToHexString(b As Byte) As String
Dim s As String = Convert.ToString(b, 16)
s = s.PadLeft(2, "0"c)
Return s
End Function
Dim hex As String() = Array.ConvertAll(bytes, AddressOf ConvertByteToHexString)
The next step is to join all those 2-digit hex numbers together into a single String to display in a TextBox. The obvious choice would be to separate them with spaces:
Me.TextBox1.Text = String.Join(" ", hex)
There you go: in a few lines of code you have displayed the contents of ANY file in a TextBox in hexadecimal form. The steps are read, convert and join.
You use basically the opposite steps to get the data back into a file: split, convert and write. The only issue is, you can't be sure that the data in the TextBox is valid, because the user could enter anything. With that in mind, you need to add a validation step between the split and convert steps:
Dim hex As String() = Me.TextBox1.Text.Split(" "c)
Dim bytes As New List(Of Byte)
Dim b As Byte
Dim allValid As Boolean = True
For Each s As String In hex
If s.Length = 2 AndAlso Integer.TryParse(s, Globalization.NumberStyles.AllowHexSpecifier, Nothing, b) Then
bytes.Add(b)
Else
allValid = False
Exit For
End If
Next
If allValid Then
IO.File.WriteAllBytes("file path here", bytes.ToArray())
Else
'Notify user of error.
End If
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.