ivanc
asked on
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
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
ASKER
Wes
Then what is a byte data?
Ivanc
Then what is a byte data?
Ivanc
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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
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
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
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
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
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
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
byData(0) = 0
byData(1) = 255
byData(2) = 123
max value of a byte is 255
ie
11111111 in binary
Gordon
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
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
ASKER
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
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
But is the string like
"01010101" or
an actual string of binary data.
if the later then
byData = StrConv(<YourData>,vbFromU nicode)
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
"01010101" or
an actual string of binary data.
if the later then
byData = StrConv(<YourData>,vbFromU
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
ASKER
Gordonp
is unicode a byte data??
Tks
Ivanc
is unicode a byte data??
Tks
Ivanc
Sorry, your comment appeared while I was posting mine.
ASKER
Gordonp
Can you repost your comment
Ivanc
Can you repost your comment
Ivanc
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
---------------
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
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
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
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
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
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
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
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
byValue = byValue * 2 + (Asc(sChar) - Asc("0"))
Wes
Now I understand. You handle 1-byte "words" one at a time. Yes, that's handy.
Wes
Wes
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
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
ASKER
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
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
Is there any particular kind of manipulation you're attempting? What is the goal of your application?
Wes
Wes
ASKER
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
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
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
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
ASKER
Guys
GordonP's solution is working fine but as I was asking how do I test for the funny character ||.
Tks
Ivanc
GordonP's solution is working fine but as I was asking how do I test for the funny character ||.
Tks
Ivanc
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
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
Maybe you could modify:
If sChar <> " " Then
to:
If Asc(sChar) <> 10 and Asc(sChar0 <> 32 Then
Wes
ASKER
Gordonp
I should have thought of that but your
PUT #1,bydata
doesn't seem to work
Ivanc
I should have thought of that but your
PUT #1,bydata
doesn't seem to work
Ivanc
Sorry, I posted at the same time but Gordon is right; it would be easier to just test for "1" and "0"
Sorry Should be
Put #1,,byData
Gordon
Put #1,,byData
Gordon
ASKER
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
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
ASKER
Gordonp
Ignore my last question but does the PUT append the or does it overwrite?
Tks
Ivanc
Ignore my last question but does the PUT append the or does it overwrite?
Tks
Ivanc
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.
ASKER
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
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
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
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
ASKER
GordonP and Wes
Tks for all the help you guys have rendered.
Ivanc
Tks for all the help you guys have rendered.
Ivanc
Wes