Loading in a binary file written in BlitzMax

Monkey Forums/Monkey Programming/Loading in a binary file written in BlitzMax

maverick69(Posted 2012) [#1]
Hi,

I have a binary file of floats I wrote with Blitzmax Stream Methods (4 byte floats). Now I'm trying to load in the binary file as string then convert it back to floats. But it doesn't work:

Local mystring$
Local floatStr$ = mystring[0..4]
Local float# = Float(floatStr)


Any ideas. I also tried with swapping the bytes before casting but didn't work either.


outsider(Posted 2012) [#2]
Local float# = Float(floatStr)

float is a name of type ;)

Try that:
Local mystring$
Local floatStr$ = mystring[0..4]
Local floatVal# = Float(floatStr)



maverick69(Posted 2012) [#3]
Doesn't work either.


muddy_shoes(Posted 2012) [#4]
If it is "binary" then it is presumably not a text representation of a float, so trying to load it as a string and then cast it is never going to work.


Samah(Posted 2012) [#5]
I'm guessing you'd have to write native code to convert it, unless you're willing to manually implement this in pure Monkey:
http://en.wikipedia.org/wiki/IEEE_floating_point


AdamRedwoods(Posted 2012) [#6]
I usually convert my binary files to base64.
Look in the monkey code forum for code. I also have an int to float converter in there.


Samah(Posted 2012) [#7]
@AdamRedwoods: Look in the monkey code forum for code.

Diddy has support for base64 conversion.


zoqfotpik(Posted 2012) [#8]
Here's a real ghetto solution that may or may not work for your application.

You could possibly write out your numbers as Monkey code to throw numbers into your data structure, but all the numbers are specified in generated code with a function to load the numbers into memory.

Then you import that generated code file and run it when you want to initialize your data structure.

I'm doing something similar to this. It's obviously not an ideal solution but has some advantages.

[Will no longer need to do it when Monkey gets render to texture...]


Sub_Zero(Posted 2012) [#9]
try this:

Strict

Import mojo

Function Main:Int()
	New Game()
	Return 0
End

Class Game Extends App

	Field thestring:String
	Field x:Int

	Method OnCreate:Int()
		Local stringValues:Int[thestring.Length()]
		For x = 0 To thestring.Length()-1
			stringValues[x] = thestring[x]
		Next
		Return 0
	End Method
	
End Class


Now you have the bytes in an int array... From here you can convert it into floats...


Samah(Posted 2012) [#10]
@Sub_Zero: From here you can convert it into floats...

This is the problem. It's not as simple as casting 32 bits to a float. You'll need to do a proper conversion to the IEEE format, which is what I linked in my post.


Sub_Zero(Posted 2012) [#11]
Try something like this: http://www.monkeycoder.co.nz/Community/posts.php?topic=2451


Sub_Zero(Posted 2012) [#12]
Oh, and once you have the bytes in an array... You can convert it to int, and then look at the previous post....

Here's some java code to convert bytes to an int:

_int = ((_sbytes[3] & 0xff) << 24) | ((_sbytes[2] & 0xff) << 16) | ((_sbytes[1] & 0xff) << 8) | (_sbytes[0] & 0xff);


It is big endian, reverse the byte order for little endian


Floyd(Posted 2012) [#13]
There's some relevant information here.

It turns out there is a data buffer class buried in the opengl module, which I had never noticed.