Results 1 to 16 of 16

Thread: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

    Hi, friends!

    I'm trying to create an application in VB6 that converts hexadecimal data printed in a rich textbox into a binary file. Some time ago, with the help of this forum, I created a similar application in .NET.

    To do the same in VB6, I researched this forum and found a very well developed code. My problem is in the data types. The code in my program is:

    Code:
    'Button code
    
    Private Sub Command2_Click()
    
    Dim Fno As Long
    Dim ByteArray() As Byte
    Dim i As Long
    
    Fno = FreeFile
    
     ReDim ByteArray(VBA.Len(RichTextBox1.Text))
    
    For i = 0 To VBA.Len(RichTextBox1.Text) - 1
        ByteArray(i) = HexToDec(VBA.Mid(RichTextBox1, i + 1, 1))
    Next i
     
    
    Open App.Path & "\testfile.bin" For Binary As #Fno
         Put #Fno, , ByteArray
    Close #Fno
    
    End Sub
    
    
    'Function 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 = VBA.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

    The hexadecimal data I want to convert to binary it is in the format I show below:

    IMPORTANT! this file contains more than 280.000 characters. So, Im just show the hex data format. The hex text file with all characters will be attached to this post.

    Code:
    F3ED5631F0DF187D7BD3BF7AD3BFC9FF
    874F0600097E23666FC9FFCB7FC8E60F
    875F1600197E23666FE9FFFFFFFFFFFF
    CF0EBEEDA320FCC9C3C0006D076D07E5
    094F19CE18C91B0C6CC97D5016880A88
    0ACD1FAF321FC0211FC07EE60FD9213B
    00E7C35700FFF53A1AC3FE0FCA82003A
    15C0B7C282003A1FC0FE8A38053E0132
    93C0F1ED453E8232FFFFCD029E2100C0
    1101C001FF1F75EDB0CD4103CD50033E
    8232FFFF31F0DFCD029ECD6B02210000
    110040010038CD8401FBCDF602C35300
    F5C5D5E5D908F5C5D5E5DDE5FDE5DBBF
    DBDDE6102196C04E77A9A1C29F003AFF
    FFF53A08C00FF5DCF701CDB341F10FF5
    CD6703CD7C10F10FF5CD4F26F10F3A1F
    C0212701DC1B003E8232FFFFCD4F98AF
    3208C0F132FFFFFDE1DDE1E1D1C1F1D9
    08E1D1C1F1FBC942084208350A011ACD

    If I declare "i" as Integer, I have an exception of type "overflow", because the hexadecimal data goes beyond the maximum accepted by integer values. So I declared "i" as Long.

    But my program crashes in runtime and I can't et the bin file.

    When I try to use dbBigint, the build points to a "user-defined type" error. The following is the image of the libraries I am using.



    Name:  libraries.png
Views: 500
Size:  11.0 KB





    I am using Visual Basic 6 on Windows 7 Ultimate 64 bit.



    Is there any solution to my problem?


    Thanks in advance.
    Attached Files Attached Files
    Last edited by vbnewbieuser; Jul 24th, 2019 at 01:44 PM. Reason: Resolved

  2. #2
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    This is what I use:
    Code:
    Public Function HexToByte(HexStr As String) As Byte()
        Dim lLen As Long
        Dim lPntr As Long
        Dim bTmp() As Byte
        Dim bHex() As Byte
        If Len(HexStr) > 1 Then
            lLen = Len(HexStr) / 2
            ReDim bHex(lLen - 1)
            For lPntr = 0 To UBound(bHex)
                bHex(lPntr) = Val("&H" & Mid$(HexStr, lPntr * 2 + 1, 2))
            Next lPntr
            HexToByte = bHex
        Else
            bHex = bTmp
        End If
    End Function
    J.A. Coutts

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    At first glance, that HexToDec function seems pretty bizarre. Why would we get into a Double when doing this stuff? Also, "Dec" makes me think of a Decimal-type rather than a Double-type.

    Also, converting from hex is far easier than what you've done. Here's a way I might recommend:

    Code:
    
    Option Explicit
    '
    
    Public Function HexToByte(ByVal HexStr As String) As Byte
        ' Will work with either 1 or 2 hex characters, must fit into a Byte.
        HexToByte = CByte("&h" & HexStr)
    End Function
    
    
    Private Sub Form_Load()
        ' Testing.
        Debug.Print HexToByte("A")
        Debug.Print HexToByte("a")
        Debug.Print HexToByte("8")
    End Sub
    
    

    Maybe that will help.

    Elroy

    EDIT1: Ahhh, I see that couttsj had the same idea. Also, he took it two-hex-characters at a time, which is more efficient.

    EDIT2: Also, I don't see anyone dealing with vbCr or vbLf characters. Those must be dealt with if things are to work correctly.
    Last edited by Elroy; Jul 24th, 2019 at 10:55 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by couttsj View Post
    This is what I use:
    Code:
    Public Function HexToByte(HexStr As String) As Byte()
        Dim lLen As Long
        Dim lPntr As Long
        Dim bTmp() As Byte
        Dim bHex() As Byte
        If Len(HexStr) > 1 Then
            lLen = Len(HexStr) / 2
            ReDim bHex(lLen - 1)
            For lPntr = 0 To UBound(bHex)
                bHex(lPntr) = Val("&H" & Mid$(HexStr, lPntr * 2 + 1, 2))
            Next lPntr
            HexToByte = bHex
        Else
            bHex = bTmp
        End If
    End Function
    J.A. Coutts
    Sir. J.A Coutts. I'm really apreciate you help. But, unfortunately, program still crashes.

    Thank you very much.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by Elroy View Post
    At first glance, that HexToDec function seems pretty bizarre. Why would we get into a Double when doing this stuff? Also, "Dec" makes me think of a Decimal-type rather than a Double-type.

    Also, converting from hex is far easier than what you've done. Here's a way I might recommend:

    Code:
    
    Option Explicit
    '
    
    Public Function HexToByte(ByVal HexStr As String) As Byte
        ' Will work with either 1 or 2 hex characters, must fit into a Byte.
        HexToByte = CByte("&h" & HexStr)
    End Function
    
    
    Private Sub Form_Load()
        ' Testing.
        Debug.Print HexToByte("A")
        Debug.Print HexToByte("a")
        Debug.Print HexToByte("8")
    End Sub
    
    

    Maybe that will help.

    Elroy
    [...]
    Sir Elroy. I'm really appreciate your help. Your code gave me compiler errors.

    Thank you.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    vbnewbieuser,

    My code shouldn't have given you any compiler errors. Are we talking about VB6? Have you installed VB6 SP6 (although I'm not sure that would make any difference for that snippet of code).

    Something is amiss here.

    Best,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    @Elroy, my guess is that both yours & couttsj offerings are being dumped in existing project without adjusting. The calling routine for OP's original method is expecting double, whereas yours returns byte and couttsj returns an array.

    Aside: carriage returns are not being addressed as you pointed out in one your replies
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    706

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    I see several problems:

    1. Your Command2_Click code is only supplying half a byte to HexToDec(), and you are looping half a byte at a time. You need to use Step 2 in your For loop, and change the last parameter of Mid to 2, so it returns 2 characters, representing one byte.
    2. You are not filtering for CR/LF or other illegal characters, so you need to check for vbCrLf at least.
    3. Your byte array is more than twice than it should be, because 2 Characters = 1 Byte, and CR/LF that are part of the RichTextBox. You can shrink the array later by using ReDim Preserve.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by LaVolpe View Post
    @Elroy, my guess is that both yours & couttsj offerings are being dumped in existing project without adjusting.
    Yeah, you may be right. You know, I've written my share of code for nothing but posting onto these forums. And, I do try my best to stay upbeat. But it does hit me wrong when someone just appears to be completely clueless, and worse, not willing to try and take our suggestions, seeing how they're pointing to a solution to their problems.

    I'm sure it would take me (or you, or Coutts, or qvb6) about 15 minutes to just write a complete solution for Newbie ... but, for the most part, I really don't see that as my purpose here. If people don't show a willingness to learn about VB6, I do start to lose interest in the thread.

    All The Best,
    Elroy

    EDIT1: And yeah, as both Coutts and qvb6 have suggested, this whole endeavor should be handled as "Bytes", and not as "Nibbles". In the OP (post #1), it's really being dealt with as nibbles, which really isn't very efficient. Newbie, a byte, when represented as hex, is two characters (ranging from 00 to ff). Therefore, if our hex is all in a string, we'd do better to take it as two (not one) hex characters at a time. Once that hex is converted to binary (say, in a byte array), then we can write it to disk as binary (machine code, or binary data, or whatever it is). Just saying.

    EDIT2: And, Newbie, if you're certain there's nothing but hex characters and line terminations in your string, you could get the line terminations out with the following lines:

    Code:
    
        Dim s As String
        ' Put your hex file into s.
    
    
        s = Replace$(s, vbLf, vbNullString)
        s = Replace$(s, vbCr, vbNullString)
    
    
    Last edited by Elroy; Jul 24th, 2019 at 12:53 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by qvb6 View Post
    I see several problems:

    1. Your Command2_Click code is only supplying half a byte to HexToDec(), and you are looping half a byte at a time. You need to use Step 2 in your For loop, and change the last parameter of Mid to 2, so it returns 2 characters, representing one byte.
    2. You are not filtering for CR/LF or other illegal characters, so you need to check for vbCrLf at least.
    3. Your byte array is more than twice than it should be, because 2 Characters = 1 Byte, and CR/LF that are part of the RichTextBox. You can shrink the array later by using ReDim Preserve.
    Sir qvb6. I'm doing this changes. And your tip about Redim preserve help me. Thank you very much, Sir.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by couttsj View Post
    This is what I use:
    Code:
    Public Function HexToByte(HexStr As String) As Byte()
        Dim lLen As Long
        Dim lPntr As Long
        Dim bTmp() As Byte
        Dim bHex() As Byte
        If Len(HexStr) > 1 Then
            lLen = Len(HexStr) / 2
            ReDim bHex(lLen - 1)
            For lPntr = 0 To UBound(bHex)
                bHex(lPntr) = Val("&H" & Mid$(HexStr, lPntr * 2 + 1, 2))
            Next lPntr
            HexToByte = bHex
        Else
            bHex = bTmp
        End If
    End Function
    J.A. Coutts

    Sir. Coutts, your function now totally works. Thank you very much!

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by Elroy View Post
    EDIT2: And, Newbie, if you're certain there's nothing but hex characters and line terminations in your string, you could get the line terminations out with the following lines:

    Code:
    
        Dim s As String
        ' Put your hex file into s.
    
    
        s = Replace$(s, vbLf, vbNullString)
        s = Replace$(s, vbCr, vbNullString)
    
    
    Sir. Elroy your help fixed my code. Thank you very much!

    And regarding your previous question, yes, I'm using VB6 SP6. I can not explain. But when I used the code you wrote to return hexadecimal values when the form is loaded, I get an error message saying "can't assign a array" (something like this, my VB6 is in spanish and i speak portuguese).

    The idiomatic context of the English language is something I don't understand well. I understand many foreign languages ​​as self-taught, English is one of these. My knowledge of languages ​​is very "instrumental", if I may put it this way. But in one of your comments, if I don't get it wrong, you talk about people who don't try to learn correctly, who don't study language correctly.

    And you are right. I agree with you.

    Although I have taken courses in computer maintenance, electronics and electricity as well as basic courses in computer programming and web pages, my knowledge needs to improve greatly. I am a self-taught in everything in life.

    I don't want to make dramas and ask them to pity me. Because there are people with more difficulties than me in this world and these people can achieve their goals. However, I live in a very complicated country that is even more complicated in recent years. Although I look like a guy who is 27 years old, I'm almost 39. In my life, things have gotten very difficult, many problems. I couldn't graduate in the areas I dreamed of, because their access is usually directed to richer people who have better educational preparation. Here, public education is a catastrophe. If public education in countries like USA, England have problems, I can say that in my country, these difficulties are raised tenfold.

    Like I said, I don't want to do dramas, knowing that there are people living in countries with worse problems than mine. But right now, for me, things are complicated about time, finances. My country may collapse soon. But still I'm trying. I am struggling to achieve my goals. I hope to be able to study computer engineering one day, even though I'm even older.

    Forgive me the big text and once again, thanks for helping me.

    And sure, case resolved again.

    Best Wishes.

  13. #13
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

    This bit of code converts the text file to binary.
    Code:
    Private Sub Command1_Click()
        Dim InputFileName As String
        Dim OutputFileName As String
        Dim sTmp As String
        Dim bTmp() As Byte
        InputFileName = "C:\Temp\New\hexdatafullcontent.txt"
        OutputFileName = "C:\Temp\New\hexdatafullcontent.bin"
        Open InputFileName For Input As #1
        Open OutputFileName For Binary Lock Write As #2
        While Not EOF(1)
            Line Input #1, sTmp
            Debug.Print sTmp
            bTmp = HexToByte(sTmp)
            Put #2, , bTmp
        Wend
        Close #1
        Close #2
    End Sub
    J.A. Coutts

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

    vbnewbieuser, the best to you as well.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

    Sure seems like an obvious job for CryptStringToBinaryW, what am I missing?

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.

    Quote Originally Posted by couttsj View Post
    This bit of code converts the text file to binary.
    Code:
    Private Sub Command1_Click()
        Dim InputFileName As String
        Dim OutputFileName As String
        Dim sTmp As String
        Dim bTmp() As Byte
        InputFileName = "C:\Temp\New\hexdatafullcontent.txt"
        OutputFileName = "C:\Temp\New\hexdatafullcontent.bin"
        Open InputFileName For Input As #1
        Open OutputFileName For Binary Lock Write As #2
        While Not EOF(1)
            Line Input #1, sTmp
            Debug.Print sTmp
            bTmp = HexToByte(sTmp)
            Put #2, , bTmp
        Wend
        Close #1
        Close #2
    End Sub
    J.A. Coutts
    Awesome, Sir J.A. Coutts! with this code, also the code suggested by Sir. Elroy, my program works according to an idea I wish I had put into practice a long time ago. Now this idea is realized, thanks to the help of all of you.

    Forgive me for taking too long to answer, work time.

    One more time, thanks!

    Best Wishes.

Tags for this Thread

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