Staredit Network

Staredit Network -> Computers and Technical -> Loading Binary Files
Report, edit, etc...Posted by O)FaRTy1billion on 2007-01-19 at 00:14:56
I'm sure I have asked this multiple times in the past, but does anyone know how?
..I'd use VB.NET, but I want to start using 6.0 because I doubt people want to download the freaking huge framework thingy just for a stupid program by me. (And 6 has shape objects biggrin.gif)


ADDITION:
Eh... I kinda posted this a bit too quickly before I completely checked it out... tongue.gif Meh, I'll leave it open just in case you or someone has any tips or anything that I should know...
Report, edit, etc...Posted by MindArchon on 2007-01-19 at 02:36:59
To load a binary file, you need to first read a binary file tongue.gif, so open it like you would when reading/writing any other file in Visual Basic.

CODE

Dim FreeF as Long
FreeF = FreeFile

Open App.Path & "\LookABinaryFile.file" for Binary as #FreeF


Now that you have the file open, there are two functions you can use. Since you are reading, you probably only need one, so that's the only one I'll go over.

CODE
Get Filenumber, Byteposition, Variable


What this code segment does, is it grabs some data in the file and sticks in Variable. The length of how much you want is dependent on the type of variable you use. For example:

CODE

Dim LongVar as Long

Get #FreeF, 1, LongVar


This code segment would take the first 4 bytes of the file we opened above, and put it in the vairable LongVar. Since LongVar is a long, the value is automatically converted to a DWORD. For the variable length, you can use arrays as well.

To read a string, you first must identify how long of a string you want to grab.

CODE
Dim MyString as string

MyString = Space(10)


Use MyString in the Get function, and it would read 10 bytes into it.

Frankly, I find that using byte arrays are the best way to go. If you're trying to read a Starcraft scenario file, your best way would be to loop through each section, and sticking the section inside a corresponding array. An example would be something like this.

CODE


Private sType() as Byte

Dim SectionName as String
Dim SectionLength as Long

SectionName = Space$(4)

Get #FreeF, Counter1, SectionName
Counter1 = Counter1 + 4

Get #FreeF, Counter1, SectionLength
Counter1 = Counter1 + 4

Select Case SectionName

Case = "TYPE"

ReDim sType(SectionLength + 7)
Get #FreeF, Counter1 - 8, sType

... repeat for all sections

End Select

Counter1 = Counter1 + SectionLength


Explanation time smile.gif. Basically, this would be part of a loop throughout the scenario file. Since there is a bunch of sections, we can find the section name and section length by using the Get function. We then create a new array, and make it - 7 because our arrays are 0 based. If we made it - 8 we would have an extra empty byte at the end.

We then use the Get function to stick the entire section in our TYPE array.

If you ARE trying to read scenario files, I recommend taking a look at the OSMAP source.

If you are trying to read a different type of Binary file, if you give me more information I can probably give you some more help based on your actual scenario.

Good luck smile.gif

Report, edit, etc...Posted by O)FaRTy1billion on 2007-01-19 at 02:55:53
Damn. O.o Now I'm told you can load binary data right into a variable... I made a function to convert a string into a long. Yay, this is simpler.

Yeah, this isn't for maps. I can figure it out from here. Yaay. tongue.gif


Pretty much all I am doing right now is making a program to look at certain *.bin files, but I got sick of using a hex-editor. If my idea on how to load them doesn't work I'll probably ask for ideas.

EDIT:
Oh, and do you know why the Combo1_changed() sub isn't doinga anything at all? O.o I place a combo dropdown box or whatever "Combo1", then I double click it to get to "Sub Combo1_changed()" and type "MsgBox ("Toadstools")" (or some other random MsgBox) and nothing happens when I change Combo1... I can't really give any more information than that because that is basically all there is to it...
Report, edit, etc...Posted by MindArchon on 2007-01-19 at 04:41:53
If I remember, "Change" only executes when the combo is text, and the user enters their own custom value.

Use "Click" instead smile.gif
Report, edit, etc...Posted by O)FaRTy1billion on 2007-01-19 at 15:26:03
Ah. I was thinking "Click" as in you just click the thing and it runs.

Hooray for persons who know what they are doing.


ADDITION:
How do you load a null-terminated string? I just want to put it in a text-box...

EDIT:
Eh. I put space(128).. but I still would like to know if there is a better way just in case a string is much longer than 128..
Report, edit, etc...Posted by MindArchon on 2007-01-19 at 18:16:56
To grab a null terminated string, I actually wouldn't use String = Space(#). I would do something like this:

CODE

Dim BytePosition as Long, GrabByte as Byte, RunningString as String
BytePosition = Wherever the string starts

Do

Get #FreeF, BytePosition, GrabByte

If GrabByte = 0 Then Exit Do

RunningString = RunningString & Chr(GrabByte)
BytePosition = BytePosition + 1

Loop


Basically, I grab one character at a time, and add it onto a string. Once the character byte is 0, its null terminated, so I exit the loop.
Report, edit, etc...Posted by ShadowFlare on 2007-01-20 at 17:00:46
In case you do need to convert binary data from a byte array or a string to an integer or long value and back, I have some functions I wrote for VB4-6 that are up on my site. smile.gif They basically use a Windows API function to copy it.
Report, edit, etc...Posted by O)FaRTy1billion on 2007-01-20 at 23:44:31
Is it possable to get unsigned Integers and Longs?
Report, edit, etc...Posted by ShadowFlare on 2007-01-21 at 00:42:35
Sort of. If you use the type of code I have up on my site (which was from my programs), you can read an unsigned 2-byte Integer value into a signed Long and the unsigned 4-byte Long into a Currency value, so that they don't exceed the limits on the existing variable types and also don't become negative values.
Next Page (1)