• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Programming A Compiler

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
ivanc
Asked:
ivanc
  • 15
  • 12
  • 12
1 Solution
 
wesleystewartCommented:
The binary data type in VB is boolean, which evaluates to 0 or 1, False or True, No or Yes.

Wes
0
 
ivancAuthor Commented:
Wes
Then what is a byte data?
Ivanc
0
 
wesleystewartCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
ivancAuthor Commented:
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
 
GordonpCommented:
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
 
ivancAuthor Commented:
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
 
ivancAuthor Commented:
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
 
wesleystewartCommented:
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
 
GordonpCommented:
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
 
GordonpCommented:
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
 
wesleystewartCommented:
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
 
ivancAuthor Commented:
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
 
GordonpCommented:
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
 
ivancAuthor Commented:
Gordonp

is unicode a byte data??

Tks
Ivanc
0
 
GordonpCommented:
Sorry, your comment appeared while I was posting mine.

0
 
ivancAuthor Commented:
Gordonp

Can you repost your comment

Ivanc
0
 
GordonpCommented:
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
 
wesleystewartCommented:
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
 
wesleystewartCommented:
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
 
GordonpCommented:
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
 
wesleystewartCommented:
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
 
wesleystewartCommented:
Now I understand.  You handle 1-byte "words" one at a time.  Yes, that's handy.

Wes
0
 
GordonpCommented:
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
 
ivancAuthor Commented:
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
 
wesleystewartCommented:
Is there any particular kind of manipulation you're attempting?  What is the goal of your application?

Wes
0
 
ivancAuthor Commented:
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
 
wesleystewartCommented:
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
 
ivancAuthor Commented:
Guys

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

Tks

Ivanc
0
 
GordonpCommented:


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
 
wesleystewartCommented:
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
 
ivancAuthor Commented:
Gordonp
I should have thought of that but your
PUT #1,bydata
doesn't seem to work

Ivanc
0
 
wesleystewartCommented:
Sorry, I posted at the same time but Gordon is right; it would be easier to just test for "1" and "0"
0
 
GordonpCommented:
Sorry Should be

Put #1,,byData

Gordon

0
 
ivancAuthor Commented:
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
 
ivancAuthor Commented:
Gordonp

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

Tks

Ivanc
0
 
GordonpCommented:

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
 
ivancAuthor Commented:
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
 
GordonpCommented:
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
 
ivancAuthor Commented:
GordonP and Wes

Tks for all the help you guys have rendered.

Ivanc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 15
  • 12
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now