Writing hex values to a binary file?

kLAcK

New member
Joined
Feb 6, 2008
Messages
4
Programming Experience
1-3
I need to write a program to patch a binary file. There are hex values that I need to find and replace in the file. I am somewhat familiar with FileStream, BinaryWriter and BinaryWriter. But I cannot for the life of me seem to write hex values to a file. Can someone point in the right direction? I feel that I am almost there... here is my code so far:

VB.NET:
Dim fs As New FileStream("Test.bin", FileMode.OpenOrCreate)
Dim w As New BinaryWriter(fs)
Dim s As String

s = "48656C6C6F20576F726C6421"
'This would be: Hello World! in hex
w.Write(s)
w.Close()
fs.Close()

Of course this code does not work, if I open Test.bin the hex values written are: 18343836353643364336463230353736463732364336343231
 
Unless you know the exact structure and interpretation of the binary file you can consider it as a long stream of byte values.
Hex is a common view for "File Viewer" applications, because many byte values represent unprintable characters they display the hex string of each byte value in order to read it. 18343836353643364336463230353736463732364336343231 doesn't really make any sense, it is 1,8,3,4 or 18,34 or 183,4 etc? You can't know. Since a byte have value between 0 and 255 even with a space between each value it become a mess, with hex both 1, 2 and 3 digit numbers may be displayed as a two letter value.
 
Hi thanks for the reply. In the example above, 18,34,38,36,35,36,43,36,43,36,46,32,30,35,37,36,46,37,32,36,43,36,34,32,31 are all two letter hex values, the incorrect hex values that I am not trying to write. I am trying to write 48,65,6C,6C,6F,20,57,6F,72,6C,64,21

I know what sequence of hex values I am looking for in the file, and what I need to replace them with. I guess what I am asking is, how do I convert hex values, into byte values? Because the BinaryWriter's method of .write uses byte values, not hex values.
 
Why convert bytes numbers to hex numbers and back to byte numbers, when you only need to work with the byte numbers the file consist of?
 
The reason I need to convert back and forth is because I am use to doing this manually in a hex editor. Is there not any easy conversion for hex to byte?
 
Back
Top