Loading in a binary file written in BlitzMax
Monkey Forums/Monkey Programming/Loading in a binary file written in BlitzMax
| ||
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. |
| ||
Local float# = Float(floatStr) float is a name of type ;) Try that: Local mystring$ Local floatStr$ = mystring[0..4] Local floatVal# = Float(floatStr) |
| ||
Doesn't work either. |
| ||
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. |
| ||
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 |
| ||
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. |
| ||
@AdamRedwoods: Look in the monkey code forum for code. Diddy has support for base64 conversion. |
| ||
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...] |
| ||
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... |
| ||
@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. |
| ||
Try something like this: http://www.monkeycoder.co.nz/Community/posts.php?topic=2451 |
| ||
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 |
| ||
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. |