Solved

# exe to binary digit data string problem - 250 pts

Posted on 2003-11-09
328 Views
hey guys, ive asked about this before, and its been slow getting responses... So i poked around, and i got alot closer. Here is my problem... I can read the exe file, and get the byte array, but when i convert it to string, it only shows the first number in the binary digit. Here is my code:

(***CODE***)
Dim BinaryString As String
Function BinaryToString(ByVal Binary As Array)
'Antonin Foller, http://www.pstruh.cz
'Optimized version of a simple BinaryToString algorithm.

Dim cl1, cl2, cl3, pl1, pl2, pl3
Dim L
cl1 = 1
cl2 = 1
cl3 = 1
L = Binary.Length

Do While cl1 < L
pl3 = pl3 & Chr(Asc(Binary(cl1))) & ","
cl1 = cl1 + 1
cl3 = cl3 + 1
If cl3 > 300 Then
pl2 = pl2 & pl3
pl3 = ""
cl3 = 1
cl2 = cl2 + 1
If cl2 > 200 Then
pl1 = pl1 & pl2
pl2 = ""
cl2 = 1
End If
End If
Loop
BinaryString = pl1 & pl2 & pl3
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TheBytes(BR.BaseStream.Length) As Byte

BinaryToString(TheBytes)
TextBox2.Text = BinaryString

'BinaryToString(TheBytes)
'TextBox3.Text = BinaryString
End Sub
End Class
(***END CODE***)

Now, when i run this, in txtbox1, i have the path to this program. When i click the button, the onclick event gets trigggered. It then reads the exe into a byte array, and converts it. Here is the output it gave me (well, the first 10 values):

7,9,6,1,5,0,2,0,3,0,

Here is the expected output (again, the first ten values):

77,90,68,1,5,0,2,0,32,0,

Now, you may have noticed my problem, the produced output, is different, in that it only displays one digit. The expected has many values that have 2 digits. The first digit in the produced o/p is the same as the first digit in the expected o/p. Now, i know this for sure, cause i went through the (litteraly thousands of) numbers outputted and compared.

Anyone wanna hint me as to why it is doing this? or gimme a fix for it?

***ALL my points going to this question, cause it is important***
0
Question by:Otis_04
• 4
• 3
• 3
• +1

LVL 17

Expert Comment

ID: 9709474
>> pl3 = pl3 & Chr(Asc(Binary(cl1))) & ","

What is happening on that line is this:

cl1=0 (It starts from 1 in your example, but the first byte in the array is 0)

Binary(cl1)=90 (This is ASC() for "M" since VB applications start with "MZ")

Asc(Binary(cl1))=57 (Asc() returns the decimal (ascii) value for "90". Since it can only return the decimal for a single character, it returns the decimal for "9" (the first one))

Chr(Asc(Binary(cl1)))=9 (The previous returned the decimal for "9", Chr() returns the character of the ASCII decimal provided which is "57" which will return the character "9")

Modified example below.

Form1:
---------------------------------

Option Explicit

Public BinaryString As String
Private Sub BinaryToString(ByRef Binary() As Byte)
Dim cl1, cl2, cl3, pl1, pl2, pl3, L
cl1 = 1
cl2 = 1
cl3 = 1
L = UBound(Binary) + 1
Do While cl1 <= L
pl3 = pl3 & Chr(Binary(cl1 - 1))
cl1 = cl1 + 1
cl3 = cl3 + 1
If cl3 > 300 Then
pl2 = pl2 & pl3
pl3 = ""
cl3 = 1
cl2 = cl2 + 1
If cl2 > 200 Then
pl1 = pl1 & pl2
pl2 = ""
cl2 = 1
End If
End If
Loop
BinaryString = pl1 & pl2 & pl3
End Sub
Dim iFF As Integer, bBuff() As Byte
iFF = FreeFile
Open "c:\Project1.exe" For Binary As iFF
ReDim bBuff(LOF(iFF))
Get #iFF, 1, bBuff()
Close iFF
Call BinaryToString(bBuff)
Debug.Print BinaryString
End Sub
0

LVL 76

Expert Comment

ID: 9709487
Don't  know why it's so complicated.

This does it in VB6

Function BinaryToString(Arr() As String * 1) As String
Dim i As Integer
Dim Text As String
Text = Asc(Arr(1))
For i = 1 To UBound(Arr)
Text = Text & ", " & Asc(Arr(i))
Next i
BinaryToString = Text
End Function

0

LVL 76

Expert Comment

ID: 9709488
Don't  know why it's so complicated.

This does it in VB6

Function BinaryToString(Arr() As String * 1) As String
Dim i As Integer
Dim Text As String
Text = Asc(Arr(1))
For i = 1 To UBound(Arr)
Text = Text & ", " & Asc(Arr(i))
Next i
BinaryToString = Text
End Function

0

LVL 17

Expert Comment

ID: 9709509
GrahamSkan, imagine dealing with 2MB binary files. With that example, you'll combine 1&2, 1&2&3, 1&2&3&4 and so forth. When you get above 1MB, there's alot of overhead on processing the combination of the large strings. It's the reason the function was done such as that.
0

Author Comment

ID: 9710782
yea. Ill take a look at that zzzzzooc, i think it will work :)
0

Author Comment

ID: 9710816
Did i mention i am using vb.NET ??

zzzzzooc - I replaced my binarytostring with yours and i get MZD[][]  (yes, those are boxes) It took me even further away from my results than i expected....
0

Accepted Solution

SiC_O earned 85 total points
ID: 9711221
ok, Otis_04, i suggest you give up and try to do this in delphi. It would be alot easier...
0

LVL 76

Expert Comment

ID: 9712735
Thanks zzzzzooc. I hadn't thought of that. We seem to have been using the wrong programming language anyway.
0

LVL 17

Expert Comment

ID: 9713429
If you just wanted the ASC() values of the characters then they're already stored within the array.

Change pl3 = pl3 & Chr(Binary(cl1 - 1)) -----> pl3 = pl3 & Binary(cl1 - 1) & ","

I'm sure you can adjust the above for .NET.

Is there any reason you accepted the answer to use Delphi? I'd suggest ASM since it'll process faster for your scenario.

0

Author Comment

ID: 9716755
>Is there any reason you accepted the answer to use Delphi? I'd suggest ASM since it'll process >faster for your scenario.

I cant program in asm :( and i dont plan on learning it.
0

LVL 17

Expert Comment

ID: 9716950
Well Delphi won't make a difference as VB can do just about the same. Not to mention the example for it was in VBScript. Just wondering why you accepted something that doesn't answer your question. I'd be fine if somebody provided an actual solution and it worked.
0

## Featured Post

### Suggested Solutions

Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…