Solved

exe to binary digit data string problem - 250 pts

Posted on 2003-11-09
11
329 Views
Last Modified: 2010-05-01
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 BR As New IO.BinaryReader(IO.File.OpenRead(TextBox1.Text))
        Dim TheBytes(BR.BaseStream.Length) As Byte

        'BR.Read(TheBytes, 1, BR.BaseStream.Length)
        TheBytes = BR.ReadBytes(BR.BaseStream.Length)
        BinaryToString(TheBytes)
        TextBox2.Text = BinaryString

        'BR.Read(TheBytes, 1, BR.BaseStream.Length)
        '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
Comment
Question by:Otis_04
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 17

Expert Comment

by:zzzzzooc
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
Private Sub Form_Load()
    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

by:GrahamSkan
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

by:GrahamSkan
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

by:zzzzzooc
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

by:Otis_04
ID: 9710782
yea. Ill take a look at that zzzzzooc, i think it will work :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Otis_04
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

by:
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

by:GrahamSkan
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

by:zzzzzooc
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

by:Otis_04
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

by:zzzzzooc
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now