Solved

Programming A Compiler

Posted on 2000-03-01
39
226 Views
Last Modified: 2012-05-04
I am suppose to program a Compiler of a simple CPU.  I got everything working however, I can't seem to understand how to create a binary file.

Firstly, what is the difference between a binary file and a normal txt file.  I assume is that for a binary file the values when read by a hex editor would be 1 and 0 but not a text file.  Please tell me if I am right.

The next thing is that there binary doesn't seem to exist as a data type in VB.  Is it byte?

How do I create them and store them into a file as the FSO object only write string.

Tks in advance

Ivanc
0
Comment
Question by:ivanc
  • 15
  • 12
  • 12
39 Comments
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2572492
The binary data type in VB is boolean, which evaluates to 0 or 1, False or True, No or Yes.

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2572512
Wes
Then what is a byte data?
Ivanc
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2572565
A Byte holds an integer between 0 and 255.  It holds (you guessed it) one byte of data.  It would have made more sense for it to hold integers from -128 to 127 (like Integer holds from -32,768 to 32,767) but I don't make the rules.

When you're dealing with binary files you could certainly use the byte data type, because it will hold 0 or 1 quite nicely.  You could dump a binary file into a byte array, but I suspect you already know far more about that than I do . . .

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2572590
Wes

I think you overestimated me.  The thing is that I am really new at manipulating files.  I have actually got my data into a text file of 1 and 0.  However, it is stored in a string.  I would like to know how am I going to change each byte (consist of 8 bit) to 1 and 0 according to my program.

Subsequently, how am I suppose to push it into a file.

Tks

IVanc
0
 
LVL 3

Accepted Solution

by:
Gordonp earned 200 total points
ID: 2572661
If you write your data to a byte array, you can then dump that byte array into a file.


Dim byData() as Byte

'Do stuff and put data into byte array.
'-------------------------
'Eg

Dim I as Integer
Redim byData(12000) as byte '12K
For I = 1 to 12000
   byData(i) = 'Something
Next

'-------------------------


Open "AFile.Out" For Binary Access Write as 1

Put #1,byData
Close #1

Gordon
0
 
LVL 3

Author Comment

by:ivanc
ID: 2572680
GordonP

For I = 1 to 12000
   byData(i) = 'Something
Next

What Something can I put there.
For example

Dim a as string
 a= "1"
   byData(0) = a


Does it mean that bydata(0) = 1

Tks

Ivanc



0
 
LVL 3

Author Comment

by:ivanc
ID: 2572710
GordonP

For I = 1 to 12000
   byData(i) = 'Something
Next

What Something can I put there.
For example

Dim a as string
 a= "1"
   byData(0) = a


Does it mean that bydata(0) = 1

Tks

Ivanc



0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2572766
Actually if your data is already in a string, you can just assign the byte array to the string value and VB will happily take care of it for you:

Sub DumpStringToArray()
dim strFileData as String
dim bytData as Byte()
dim lngPointer as LongstrFileData = 'get file data here.  That's your problem :-)
bytData() = strFileData
'now your string has been dumped into the byte array
For lngPointer = LBound(bytData) to UBound(bytData)
debug.Print bytData(lngPointer)
'work with each byte here
Next
End sub

Wes
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2572806
What format is your data in to start with.

I have code that copies a structure directly into a byte array.

the something in

byData(i) = Something

will be dependant on the format your data is in

do you just have the 1's and 0's or do you have the data in some other form.

Gordon
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2572841
You can assign values directly for each element of the array


byData(0) = 0
byData(1) = 255

byData(2) = 123

max value of a byte is 255
ie

11111111 in binary

Gordon
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2572874
I'm not sure I would like assigning each element one at a time.  That would be a real hoot if you got past a few dozen data elements . . .

Gordon:  
A careful read of the questioner's comments reveals:

"I have actually got my data into a text file of 1 and 0.  However, it is stored in a string"  The data already exists.  I think we're trying to find ways to access it programmatically.

Back in the old days you would store binary data like bitmaps or sound files in a string and then manipulate it after dumping it into a byte array.  This was slow and crappy, and now there are better ways.

ivanc:

What exactly are you trying to do with the data?

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2572896
Guys

To Clear the air up, My data has been stored in a file opened through FSO like this.

1111 0000 0000 1101 1111 1111 1111
..
..
..
I can retrieve this line by line and split it up into 4 "bits"

However, I don't know how to convert it into BYTE data.

Wes

dim lngPointer as LongstrFileData = 'get file data here.  That's your problem :-)

What does this mean??
strfiledata = "1111 0000"
bytData() = strFileData

Correct me if I am wrong.  the line above would change all the code to byte.
bytdata = 11110000
or does it change to the ASCII value of the string.

Gordonp

Can I assign value to byte like this

bydata(0) = 11111111

Does it mean that bydata(0) = 11111111
and in the binary file it would come out as 11111111

Tks to all

Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2572899
But is the string like

"01010101" or

an actual string of binary data.

if the later then


byData = StrConv(<YourData>,vbFromUnicode)

you can directly assign the string, but using StrConv is more efficient as your explicitly telling VB to do the Unicode to Ansii Conversion rather than letting it work that out for itself.

Gordon
0
 
LVL 3

Author Comment

by:ivanc
ID: 2572932
Gordonp

is unicode a byte data??

Tks
Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2572946
Sorry, your comment appeared while I was posting mine.

0
 
LVL 3

Author Comment

by:ivanc
ID: 2572999
Gordonp

Can you repost your comment

Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2573007
I Have Code.
---------------

    Dim StringData as String
    StringData = 'Read in your Text File

    Dim byData() As Byte

    Dim nCount As Integer
    Dim byValue As Byte
    Dim sChar As String
    Dim nIndex As Integer
   
    nIndex = 0
    byValue = 0
   
    For nCount = 1 To Len(StringData)
        sChar = Mid(StringData, nCount, 1)
        If sChar <> " " Then
            nIndex = nIndex + 1
            byValue = byValue * 2 + (Asc(sChar) - Asc("0"))
            If (nIndex Mod 8) = 0 Then
                Debug.Print byValue
                ReDim Preserve byData(nIndex / 8)
                byData(nIndex / 9) = byValue
                byValue = 0
                nIndex = 0
            End If
        End If
    Next

    'Output Byte Array as shown to file as shown in my earlier comment

GordonP
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573026
Ooops.

dim lngPointer as LongstrFileData = 'get file data here.  That's your problem :-)


Should have come out as:

dim lngPointer as Long
strFileData = 'get file data here.  That's your problem :-)

That would take each character in your string and put into one element (actually two elements) of the byte array.

I'm sure we're all willing to help out, but at this point I'd suggest you find some good programming manuals that deal with byte arrays and strings.  If you start your project with whatever understanding of the subject we give you here, I think you're going to have a LOT of problems.

Let's say strData = "Hello"
and we assign it to a byte array:
bytData() = strData

Then the elements of bytData are filled with the bytes that make up "Hello" as follows (from VBA developer's handbook):

bytData(0) = 72 (This is "H" in unicode)
bytData(1) = 0
bytData(2) = 101 (This is "e" in unicode)
bytData(3) = 0
....and so on

Because VBA stores strings in unicode every character takes up two bytes.  We use a byte array because we will never be assigning a value larger than 255 to each element of the byte array.  bytData(0) could never be 11111111 for two reasons:  11111111 is larger than 255, and it is NOT a unicode character.  If you assigned "11111111" (a string) to a byte array, each "1" would be assigned to its own elements (or two elements, as shown in the example)

Wes

0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573071
Gordon:

That is impressive, but how is that different from:

byData() = StringData

which dumps a string into a byte array?

I'm trying to run your code in a test app but am banging into overflow errors.

Wes
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:Gordonp
ID: 2573089
Because if the string is "1111 0000"

you get
byData(0) = 49
byData(1) = 49
byData(2) = 49
byData(3) = 49
byData(4) = 32
byData(5) = 48
byData(6) = 48
byData(7) = 48
byData(8) = 48

whereas my code for the same string gives

byData(0) = 240  'Binary value 11110000

Gordon

0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573090
It looks like byValue can't be a byte.  It starts to get pretty large when you execute:

byValue = byValue * 2 + (Asc(sChar) - Asc("0"))

Wes
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573100
Now I understand.  You handle 1-byte "words" one at a time.  Yes, that's handy.

Wes
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2573140
My Code parses, the string, translating "1"'s and "0"'s into 1's and 0's (and ignores spaces)

it groups the 1's and 0's into 8Bit groups ( ie a Byte)

11110000

then translates that to a value

ie

240

and puts it into the byte array


Gordon

0
 
LVL 3

Author Comment

by:ivanc
ID: 2573185
Gordonp

When I read in the string the fso file gives the enter as some funny || character.  How do I check for this character and get rid of it.

Wes

I tried your bydata() = stringdata

Type mismatch is what I get.

Actually, I do understand string and byte but I am not sure how to manipulate it with the VB codes.

Tks

Ivanc
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573196
Is there any particular kind of manipulation you're attempting?  What is the goal of your application?

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2573207
Wes

As I have state, all I am trying to do is to change my string data of 1s and 0s and Carriage return that has been stored in a txt file to a binary of byte file.

That's the aim.

Tks

Ivanc
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573214
I guess I'm asking for more specific details.  We can generate byte arrays from binary data all day long, but then what?

bytData() = stringData

Will only work if bytData has been declared as a byte array and stringData has been declared as a string and has a value.

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2573230
Guys

GordonP's solution is working fine but as I was asking how do I test for the funny character ||.

Tks

Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2573231


Ok Change

If sChar <> " " Then
           
to

If sChar = "1" or sChar = "0" Then

that way only "1"'s and "0"'s will get processed.

Gordon



0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573242
a carriage return's character is 10.  You'll have to trap it somehow within the byte conversion and leave it out.  
Maybe you could modify:

If sChar <> " " Then

to:

If Asc(sChar) <> 10 and Asc(sChar0 <> 32 Then

Wes
0
 
LVL 3

Author Comment

by:ivanc
ID: 2573246
Gordonp
I should have thought of that but your
PUT #1,bydata
doesn't seem to work

Ivanc
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2573247
Sorry, I posted at the same time but Gordon is right; it would be easier to just test for "1" and "0"
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2573261
Sorry Should be

Put #1,,byData

Gordon

0
 
LVL 3

Author Comment

by:ivanc
ID: 2573286
GordonP

I don't think the put file is working as it gives me only two squares even I have a great deal of think stored in the array.  Must I account for the array.

Ivanc
0
 
LVL 3

Author Comment

by:ivanc
ID: 2573300
Gordonp

Ignore my last question but does the PUT append the or does it overwrite?

Tks

Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2575645

If you open the file as shown above it will overwrite any previous contents.

if you want to append to the end of a file

you can open it as

Open "<Filename>" For Append Access Write as 1

Gordon

ps.

remember the file your creating only contains binary data, so dont expect to see anything intelligible if you open it in a text editor. Notpad only displays letters and numbers and a few other characters, any other byte values will be displayed as spaces.


0
 
LVL 3

Author Comment

by:ivanc
ID: 2576870
GordonP

If I open the file as write but I code as below would it write all the binary values into teh file.

    For nCount = 1 To Len(StringData)
        sChar = Mid(StringData, nCount, 1)
        If sChar = "1" Or sChar = "0" Then
            nIndex = nIndex + 1
            byValue = byValue * 2 + (Asc(sChar) - Asc("0"))
            If (nIndex Mod 8) = 0 Then
                Debug.Print byValue
                ReDim Preserve byData(nIndex / 8)
                byData(nIndex / 9) = byValue
                Put #1, , byData
                byValue = 0
                nIndex = 0
            End If
        End If
    Next
Really Thankful
Ivanc
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2579252
Not Quite,


the line Put#1,,byData is writing the whole array to the file every time.

If you change it to
   Put #1,,byData(nIndex/8)

or move the original line to after the For Next Loop

it'll do what you want.

also error in my code
byData(nIndex / 9) = byValue
should be
bydata(nIndex / 8) = byValue
               
Gordon
0
 
LVL 3

Author Comment

by:ivanc
ID: 2580777
GordonP and Wes

Tks for all the help you guys have rendered.

Ivanc
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

758 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

22 Experts available now in Live!

Get 1:1 Help Now