Results 1 to 16 of 16

Thread: Convert hex string to binary file [resolved]

  1. #1

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Resolved 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.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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?
    Chris

  3. #3

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Convert hex string to binary file

    nono, it's a real hex..

    VB Code:
    1. part(1)="00000000A75CF643000000000000030003000000580000800E00000040000080100000002800008000000000A75CF64300000000000001000100000080000080"
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  4. #4
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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:
    1. Private Sub Command1_Click
    2. Dim Fno as Integer
    3. Dim ByteArray() As Byte
    4. dim i as Integer
    5.  
    6. Fno = FreeFile
    7.  
    8. For i = 0 To Len(HEXString)
    9.     ByteArray(i) = HexToDec(Mid(HEXString, i, 1))
    10. Next i
    11.  
    12. 'Save it to file
    13. Open "c:\something.exe" For Binary As #Fno
    14.      Put #Fno, , ByteArray
    15. Close #Fno
    16. End Sub
    Put the following function either on the form or in a module...
    VB Code:
    1. Public Function HexToDec(ByVal HexStr As String) As Double
    2. Dim mult As Double
    3. Dim DecNum As Double
    4. Dim ch As String
    5. Dim i As Integer
    6. mult = 1
    7. DecNum = 0
    8. For i = Len(HexStr) To 1 Step -1
    9.     ch = Mid(HexStr, i, 1)
    10.     If (ch >= "0") And (ch <= "9") Then
    11.         DecNum = DecNum + (Val(ch) * mult)
    12.     Else
    13.         If (ch >= "A") And (ch <= "F") Then
    14.             DecNum = DecNum + ((Asc(ch) - Asc("A") + 10) * mult)
    15.         Else
    16.             If (ch >= "a") And (ch <= "f") Then
    17.                 DecNum = DecNum + ((Asc(ch) - Asc("a") + 10) * mult)
    18.             Else
    19.                 HexToDec = 0
    20.                 Exit Function
    21.             End If
    22.         End If
    23.     End If
    24.     mult = mult * 16
    25. Next i
    26. HexToDec = DecNum
    27. End Function
    Replace the filename with your file, and replace HEXString with your hex string variable
    Chris

  5. #5

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Convert hex string to binary file

    Thanks, i'm trying it now...
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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:
    1. ReDim ByteArray(Len(HEXString))

    I havent tested it but i think you will also need to change the For line to:
    VB Code:
    1. 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

  7. #7

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    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:
    1. Open "tmp001.dat" For Binary As #Fno
    2.     For a = 0 To 9
    3.         For i = 0 To Len(dat(a)) - 1
    4.             ReDim byteArray(Len(dat(a)) - 1)
    5.             byteArray(i) = HexToDec(Mid(dat(a), i, 1)) '<-- it's giving me the error in here
    6.         Next i
    7.         Put #Fno, , byteArray
    8.     Next a
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    1. Public Function Hex2ByteArr(ByVal sHex As String) As Byte()
    2.     Dim n As Long
    3.     Dim nCount As Long
    4.     Dim bArr() As Byte
    5.     'First of all, make sure the length of the hex string is even, if it is not then
    6.     'put a "0" at the beginning
    7.     nCount = Len(sHex)
    8.     If (nCount And 1) = 1 Then
    9.         sHex = "0" & sHex
    10.         nCount = nCount + 1
    11.     End If
    12.     'ReDim the Byte array
    13.     ReDim bArr(nCount \ 2 - 1) 'we subtract 1 since the array is zero based
    14.     For n = 1 To nCount Step 2
    15.         'Convert the hex numbers into decimal values and store them in the byte array
    16.         bArr((n - 1) \ 2) = CByte("&H" & Mid$(sHex, n, 2))
    17.     Next
    18.     'Return the array
    19.     Hex2ByteArr = bArr
    20. End Function
    To save this you would call this function in a simular way to this:
    VB Code:
    1. Dim bArr() As Byte
    2. Dim hFile As Integer
    3. bArr = Hex2ByteArr("00000000A75CF643")
    4. 'You should of course pass the whole hex string to the function above.
    5. '--
    6. 'Save it to a file
    7. hFile = FreeFile
    8. Open "c:\SomeFile.dat" For Binary Access Write As #hFile
    9.     Put #hFile, , bArr
    10. 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.

  9. #9
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Convert hex string to binary file

    Found it...change the
    VB Code:
    1. 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
    Chris

  10. #10

    Thread Starter
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    Re: Convert hex string to binary file

    working great now thanka a lot!
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Convert hex string to binary file

    Quote 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.

  12. #12
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Convert hex string to binary file

    Quote 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
    Chris

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  14. #14
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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
    Chris

  15. #15
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: Convert hex string to binary file

    Quote 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:
    1. Public Function Hex2ByteArr(ByVal sHex As String) As Byte()
    2.     Dim n As Long
    3.     Dim nCount As Long
    4.     Dim bArr() As Byte
    5.     'First of all, make sure the length of the hex string is even, if it is not then
    6.     'put a "0" at the beginning
    7.     nCount = Len(sHex)
    8.     If (nCount And 1) = 1 Then
    9.         sHex = "0" & sHex
    10.         nCount = nCount + 1
    11.     End If
    12.     'ReDim the Byte array
    13.     ReDim bArr(nCount \ 2 - 1) 'we subtract 1 since the array is zero based
    14.     For n = 1 To nCount Step 2
    15.         'Convert the hex numbers into decimal values and store them in the byte array
    16.         bArr((n - 1) \ 2) = CByte("&H" & Mid$(sHex, n, 2))
    17.     Next
    18.     'Return the array
    19.     Hex2ByteArr = bArr
    20. End Function
    To save this you would call this function in a simular way to this:
    VB Code:
    1. Dim bArr() As Byte
    2. Dim hFile As Integer
    3. bArr = Hex2ByteArr("00000000A75CF643")
    4. 'You should of course pass the whole hex string to the function above.
    5. '--
    6. 'Save it to a file
    7. hFile = FreeFile
    8. Open "c:\SomeFile.dat" For Binary Access Write As #hFile
    9.     Put #hFile, , bArr
    10. 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.

  16. #16
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    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

    Quote 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:
    1. Public Function Hex2ByteArr(ByVal sHex As String) As Byte()
    2.     Dim n As Long
    3.     Dim nCount As Long
    4.     Dim bArr() As Byte
    5.     'First of all, make sure the length of the hex string is even, if it is not then
    6.     'put a "0" at the beginning
    7.     nCount = Len(sHex)
    8.     If (nCount And 1) = 1 Then
    9.         sHex = "0" & sHex
    10.         nCount = nCount + 1
    11.     End If
    12.     'ReDim the Byte array
    13.     ReDim bArr(nCount \ 2 - 1) 'we subtract 1 since the array is zero based
    14.     For n = 1 To nCount Step 2
    15.         'Convert the hex numbers into decimal values and store them in the byte array
    16.         bArr((n - 1) \ 2) = CByte("&H" & Mid$(sHex, n, 2))
    17.     Next
    18.     'Return the array
    19.     Hex2ByteArr = bArr
    20. End Function
    To save this you would call this function in a simular way to this:
    VB Code:
    1. Dim bArr() As Byte
    2. Dim hFile As Integer
    3. bArr = Hex2ByteArr("00000000A75CF643")
    4. 'You should of course pass the whole hex string to the function above.
    5. '--
    6. 'Save it to a file
    7. hFile = FreeFile
    8. Open "c:\SomeFile.dat" For Binary Access Write As #hFile
    9.     Put #hFile, , bArr
    10. 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
  •  



Click Here to Expand Forum to Full Width