Is there any easy way to convert a hex string into a binary file?
Printable View
Is there any easy way to convert a hex string into a binary file?
Is the hex string truely a hex string such as '2FA10143B' or does the hex string look like the data which you find when you open a binary file such as a .exe with notepad?
nono, it's a real hex..
VB Code:
part(1)="00000000A75CF643000000000000030003000000580000800E00000040000080100000002800008000000000A75CF64300000000000001000100000080000080"
So you want to convert that hex string to binary format of a file, so you can save it to a binary file......This functioin HexToDec converts Hex values into decimal or denary values, then you can store it in a byte array which you can then use to output the converted hex to a binary file...Put the following function either on the form or in a module...VB Code:
Private Sub Command1_Click Dim Fno as Integer Dim ByteArray() As Byte dim i as Integer Fno = FreeFile For i = 0 To Len(HEXString) ByteArray(i) = HexToDec(Mid(HEXString, i, 1)) Next i 'Save it to file Open "c:\something.exe" For Binary As #Fno Put #Fno, , ByteArray Close #Fno End SubReplace the filename with your file, and replace HEXString with your hex string variable:)VB Code:
Public Function HexToDec(ByVal HexStr As String) As Double Dim mult As Double Dim DecNum As Double Dim ch As String Dim i As Integer mult = 1 DecNum = 0 For i = Len(HexStr) To 1 Step -1 ch = Mid(HexStr, i, 1) If (ch >= "0") And (ch <= "9") Then DecNum = DecNum + (Val(ch) * mult) Else If (ch >= "A") And (ch <= "F") Then DecNum = DecNum + ((Asc(ch) - Asc("A") + 10) * mult) Else If (ch >= "a") And (ch <= "f") Then DecNum = DecNum + ((Asc(ch) - Asc("a") + 10) * mult) Else HexToDec = 0 Exit Function End If End If End If mult = mult * 16 Next i HexToDec = DecNum End Function
Thanks, i'm trying it now...
Oh sorry i found a mistake...after the FreeFile line and before the for loop put
VB Code:
ReDim ByteArray(Len(HEXString))
I havent tested it but i think you will also need to change the For line to:
VB Code:
For i = 0 To Len(HEXString) - 1
Just play around with it to work out the bugs
EDIT: changed For line^ & ReDim line
i'm getting a wierd error on this...
Invalid procedure call or argument
here is my code:
VB Code:
Open "tmp001.dat" For Binary As #Fno For a = 0 To 9 For i = 0 To Len(dat(a)) - 1 ReDim byteArray(Len(dat(a)) - 1) byteArray(i) = HexToDec(Mid(dat(a), i, 1)) '<-- it's giving me the error in here Next i Put #Fno, , byteArray Next a
There are a few logical errors in that code. First of all the function should return a Byte instead of a Double since you assign it to a byte. Also since a byte value is 8 bits and that is represented by 2 hex numbers the code should look simular to this:To save this you would call this function in a simular way to this:VB Code:
Public Function Hex2ByteArr(ByVal sHex As String) As Byte() Dim n As Long Dim nCount As Long Dim bArr() As Byte 'First of all, make sure the length of the hex string is even, if it is not then 'put a "0" at the beginning nCount = Len(sHex) If (nCount And 1) = 1 Then sHex = "0" & sHex nCount = nCount + 1 End If 'ReDim the Byte array ReDim bArr(nCount \ 2 - 1) 'we subtract 1 since the array is zero based For n = 1 To nCount Step 2 'Convert the hex numbers into decimal values and store them in the byte array bArr((n - 1) \ 2) = CByte("&H" & Mid$(sHex, n, 2)) Next 'Return the array Hex2ByteArr = bArr End FunctionNow the above assumes that a file named "c:\SomeFile.dat" doesn't already exist. If it does you should delete it first.VB Code:
Dim bArr() As Byte Dim hFile As Integer bArr = Hex2ByteArr("00000000A75CF643") 'You should of course pass the whole hex string to the function above. '-- 'Save it to a file hFile = FreeFile Open "c:\SomeFile.dat" For Binary Access Write As #hFile Put #hFile, , bArr Close #hFile
Found it...change the...my fault for being lazy and coding off the top of my headVB Code:
byteArray(i) = HexToDec(Mid(dat(a), i + 1, 1))
@jaocim i took looked at a huge hex editor from pscode and it worked by only putting 1 hex character into each bytearray
If it does require 2 the 1 in the above line just needs to be changed to 2
working great now :) thanka a lot!
Well, the highest number you can type with only one hexadigital number is F which equals 15, while the highest number you can store in a byte is 255 (or FF in hex). So the hexadecimal values for a byte are in the range from 00 to FF.Quote:
Originally Posted by the182guy
Then I must of read the code wrong as the editor worked fine, so the '1' in the above line just needs to be '2' instead :)Quote:
Originally Posted by Joacim Andersson
Yes, but you must also then the next time read the next two hex numbers and not read the second character again. That is why I in my example, which was simular to yours, used Step 2 in the For loop.
Anyway, the thread has been marked as resolved.
Ah yes good point, it just needs to be modified to not read the second value....I guess vb is not your best friend after several vodkas at 3am:D
Quote:
Originally Posted by Joacim Andersson
hello,
how can we change start and end address, specific address ? :rolleyes:
B.R
VB Client/Server
OK, I already solve problem...
Stupid Q... :wave:
Just wanted to say thanks .. was searching forever for this ..
managed to use it for registry binary settings .. see here:
http://www.vbforums.com/showthread.php?t=416821
Quote:
Originally Posted by Joacim Andersson