tags:

views:

350

answers:

4

I need to parse some simple binary Files. (The files contains n entries which consists of several signed/unsigned Integers of different sizes etc.)

In the moment i do the parsing "by hand". Does somebody know a library which helps to do this type of parsing?

Edit: "By hand" means that i get the Data Byte by Byte sort it in to the correct Order and convert it to an Int/Byte etc. Also some of the Data is unsigned.

+2  A: 

I don't know what you mean with "by hand" but using a simple DataInputStream (apidoc here) is quite concise and clear:

val dis = new DataInputStream(yourSource)

dis.readFloat()
dis.readDouble()
dis.readInt()
// and so on

Taken from another SO question: http://preon.sourceforge.net/, it should be a framework to do binary encoding/decoding.. see if it has the capabilities you need

Jack
What about endianess? what about pascal strings (byte for size, and then string in ascii)? I believe he's looking for something like python's `struct.pack`,`struct.unpack`.
Elazar Leibovich
+3  A: 

Scala itself doesn't have a binary data input library, but the java.nio package does a decent job. It doesn't explicitly handle unsigned data--neither does Java, so you need to figure out how you want to manage it--but it does have convenience "get" methods that take byte order into account.

Rex Kerr
+4  A: 

I've used the sbinary library before and it's very nice. The documentation is a little sparse but I would suggest first looking at the old wiki page as that gives you a starting point. Then check out the test specifications, as that gives you some very nice examples.

The primary benefit of sbinary is that it gives you a way to describe the wire format of each object as a Format object. You can then encapsulate those formatted types in a higher level Format object and Scala does all the heavy lifting of looking up that type as long as you've included it in the current scope as an implicit object.

Aaron
+1  A: 

If you are looking for a Java based solution, then I will shamelessly plug Preon. You just annotate the in memory Java data structure, and ask Preon for a Codec, and you're done.

Wilfred Springer