Jump to content
  • Advertisement
Sign in to follow this  

loading from binary files

This topic is 5056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to load from binary files that were created in Delphi. I'm using VB 6. This is what the first two things in the file look like in Delphi:
 TMapFile_Header = packed record
   Version : integer;

 TMapFile_Options = packed record
   MapName : string[38];
   TextureName : string[24];
   BackgroundColor, BackgroundColor2 : longint;
   StartJet : integer;
   GrenadePacks, Medikits : byte;
   Weather : byte;
   Steps : byte;
   MapRandomID : integer;
So I created these in my VB program:
Private Type TMapFile_Header
    Version As Long
End Type
Private Type TMapFile_Options
    MapName As String * 38
    TextureName As String * 24
    BackgroundColor As Long
    BackgroundColor2 As Long
    StartJet As Long
    GrenadePacks As Byte
    Medikits As Byte
    Weather As Byte
    Steps As Byte
    MapRandomID As Long
End Type
The TMapFile_Header gives me the value 11, which is correct. Howevever everything in Options gets screwed up. MapName is supposed to be "Welcome to Mr. Snowman's playground" but gets loaded as "#Welcome to Mr. Snowman's playgroundtú". TextureName is supposed to be "ananas.bmp" but gets loaded as "ananas.bmp". All the other values in the TMapFile_Options type are screwed up. I think it might be because the Delphi records are "packed" records, but I have no idea what to do about this. Any help is appreciated.

Share this post

Link to post
Share on other sites
I think the problem is different. Delphi is based off of Pascal, and Pascal uses a slightly different string representation than what VB is expecting. In Pascal, the length of a string is prepended to the string followed by the string text. Whereas VB is expecting the string followed by a terminating null. This explains the garbage character in the beginning and the extra characters following the string proper.

One way to handle this difference is when defining the structure in VB is to add an extra byte to the begnning of the string, and use that string length information to remove the trailing characters after reading the struct. Alternately you can get the Delphi structure to store C style strings, though I don't remember the syntax for that.

Share this post

Link to post
Share on other sites
When it comes to reading the string in VB:
1) Retrieve the length of the string (not sure of the storage length - Byte, Word or Integer)
2) ReDim a Byte() array to this length
3) Read the string bytes into the byte array with a loop
4) Use StrConv([bytes()], vbUnicode) to convert the byte array into a VB String

Public Function ByteArrayToString(bytArray() As Byte) As String
Dim sAns As String
Dim iPos As String

sAns = StrConv(bytArray, vbUnicode)
iPos = InStr(sAns, Chr(0))
If iPos > 0 Then sAns = Left(sAns, iPos - 1)

ByteArrayToString = sAns

End Function

From FreeVBCode.com

Share this post

Link to post
Share on other sites
Thanks a lot guys!!

Here is my new code, kind of ugly but it gets the job done ^_^ (I think those are the only strings in the entire file.)
    Dim header As TMapFile_Header
Dim options As TMapFile_Options

Dim i As Integer
Dim char As Byte
Dim strLength As Byte
Dim temp As String

Open "D:\Games\Soldat\Maps\MrSnowman.PMS" For Binary Access Read Lock Read As #1

Get #1, , header
Get #1, , strLength
For i = 1 To 38
Get #1, , char
If i <= strLength Then
temp = temp + Chr$(char)
End If
options.MapName = temp

temp = ""

Get #1, , strLength
For i = 1 To 24
Get #1, , char
If i <= strLength Then
temp = temp + Chr$(char)
End If
options.TextureName = temp

Get #1, , options.BackgroundColor
Get #1, , options.BackgroundColor2
Get #1, , options.StartJet
Get #1, , options.GrenadePacks
Get #1, , options.Medikits
Get #1, , options.Weather
Get #1, , options.Steps
Get #1, , options.MapRandomID

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!