?
Solved

exe to binary digit data string problem - 250 pts

Posted on 2003-11-09
11
Medium Priority
?
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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 340 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

801 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