|
-
Feb 17th, 2006, 07:28 PM
#1
Thread Starter
Fanatic Member
Convert hex string to binary file [resolved]
Is there any easy way to convert a hex string into a binary file?
Last edited by TDQWERTY; Feb 17th, 2006 at 09:00 PM.
-
Feb 17th, 2006, 07:39 PM
#2
Re: Convert hex string to 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?
-
Feb 17th, 2006, 07:42 PM
#3
Thread Starter
Fanatic Member
Re: Convert hex string to binary file
nono, it's a real hex..
VB Code:
part(1)="00000000A75CF643000000000000030003000000580000800E00000040000080100000002800008000000000A75CF64300000000000001000100000080000080"
-
Feb 17th, 2006, 08:00 PM
#4
Re: Convert hex string to binary file
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...
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 Sub
Put the following function either on the form or in a module...
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
Replace the filename with your file, and replace HEXString with your hex string variable
-
Feb 17th, 2006, 08:13 PM
#5
Thread Starter
Fanatic Member
Re: Convert hex string to binary file
Thanks, i'm trying it now...
-
Feb 17th, 2006, 08:19 PM
#6
Re: Convert hex string to binary file
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
Last edited by the182guy; Feb 17th, 2006 at 08:27 PM.
Chris
-
Feb 17th, 2006, 08:42 PM
#7
Thread Starter
Fanatic Member
Re: Convert hex string to binary file
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
-
Feb 17th, 2006, 08:44 PM
#8
Re: Convert hex string to binary file
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:
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 Function
To save this you would call this function in a simular way to this:
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
Now the above assumes that a file named "c:\SomeFile.dat" doesn't already exist. If it does you should delete it first.
-
Feb 17th, 2006, 08:53 PM
#9
Re: Convert hex string to binary file
Found it...change the
VB Code:
byteArray(i) = HexToDec(Mid(dat(a), i + 1, 1))
...my fault for being lazy and coding off the top of my head
@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
-
Feb 17th, 2006, 09:00 PM
#10
Thread Starter
Fanatic Member
Re: Convert hex string to binary file
working great now thanka a lot!
-
Feb 17th, 2006, 09:23 PM
#11
Re: Convert hex string to binary file
 Originally Posted by the182guy
@jaocim i took looked at a huge hex editor from pscode and it worked by only putting 1 hex character into each bytearray
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.
-
Feb 17th, 2006, 09:41 PM
#12
Re: Convert hex string to binary file
 Originally Posted by Joacim Andersson
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.
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
-
Feb 17th, 2006, 09:46 PM
#13
Re: Convert hex string to binary file [resolved]
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.
-
Feb 17th, 2006, 09:54 PM
#14
Re: Convert hex string to binary file [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
-
Jun 16th, 2006, 10:24 AM
#15
Hyperactive Member
Re: Convert hex string to binary file
 Originally Posted by Joacim Andersson
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:
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 Function
To save this you would call this function in a simular way to this:
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
Now the above assumes that a file named "c:\SomeFile.dat" doesn't already exist. If it does you should delete it first.
hello,
how can we change start and end address, specific address ?
B.R
VB Client/Server
OK, I already solve problem...
Stupid Q...
Last edited by VB Client/Server; Jun 16th, 2006 at 10:44 AM.
-
Jan 2nd, 2007, 04:41 AM
#16
PowerPoster
Re: Convert hex string to binary file
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
 Originally Posted by Joacim Andersson
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:
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 Function
To save this you would call this function in a simular way to this:
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
Now the above assumes that a file named "c:\SomeFile.dat" doesn't already exist. If it does you should delete it first.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|