[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.
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.
I am using Visual Basic 6 on Windows 7 Ultimate 64 bit.
Is there any solution to my problem?
Thanks in advance.
Last edited by vbnewbieuser; Jul 24th, 2019 at 01:44 PM.
Reason: Resolved