[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
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
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.
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by couttsj
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.
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by Elroy
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.
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.
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"
Re: VB6 - Program crashes trying to convert hex data into bin file.
I see several problems:
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.
You are not filtering for CR/LF or other illegal characters, so you need to check for vbCrLf at least.
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.
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by LaVolpe
@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.
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by qvb6
I see several problems:
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.
You are not filtering for CR/LF or other illegal characters, so you need to check for vbCrLf at least.
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.
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by couttsj
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!
Re: VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by Elroy
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.
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
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.
Re: [RESOLVED] VB6 - Program crashes trying to convert hex data into bin file.
Originally Posted by couttsj
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.