Is there an equivalent in Objective C to C#'s BinaryReader and BinaryWriter? For example, a BinaryReader would take an NSData*
object (or a generalised stream), and would have methods such as -(uint32_t)readUnsignedInt
, -(double)readDouble
, etc. A BinaryWriter would be symmetrical, and would work with NSMutableData*
(or a generalised stream).
Asked
Active
Viewed 921 times
1

Vladimir Gritsenko
- 1,669
- 11
- 25
2 Answers
3
So I ended up writing a reader and writer for binary streams: https://github.com/vladimirg/objc-binary-stream-tools

Vladimir Gritsenko
- 1,669
- 11
- 25
-
I used your library but I'm getting a warning on GENERATE_METHOD(Int32, int32_t, uint32_t)//Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead and when i run it i get an EXEC_BAD_ACCESS – Legnus Jan 15 '15 at 13:18
-
@Legnus, nice to know somebody is using this! I don't have access to an x64 iPhone unfortunately, but I did fix the warnings. If you turn on exceptions breakpoints, run the test suite and tell me what the origin of EXEC_BAD_ACCESS is, that may help me fix the problem. You can open an issue for it on github. – Vladimir Gritsenko Jan 16 '15 at 12:47
1
There isn't, but it is very easy to write one.
I wrote a stream reader for decoding OWON Oscilloscope binary files. See the OwStreamingDataParser class found in this github repository. Creating the writer would be the same thing, but in reverse. That should, at least, give you a start.
Note that if you are reading/writing graphs of Objective-C objects and only targeting iOS or OS X, then you can use NSArchiver.

bbum
- 162,346
- 23
- 271
- 359
-
Easy - maybe, trivial - no :-( there are a few edge cases to handle, such as endianness (you assumed little endianness for int32 and host endianness for floats, right?). Maybe I should see this as an opportunity to write one and post it on github :-) – Vladimir Gritsenko Sep 09 '13 at 08:42
-
@VladimirGritsenko Yeah -- I made some endian assumptions that I'll likely regret once I start processing the data. :) No, not trivial, as it'd require quite a bit more code. Please do and share it (so I can leverage it in my code). – bbum Sep 09 '13 at 15:24
-
1
-
Done, please see my answer to this question. Hope you find it useful! – Vladimir Gritsenko Sep 14 '13 at 23:15
-
@VladimirGritsenko Nice! Note that `getError` should just be `error` or `lastError`. Beyond that, looks straight forward! – bbum Sep 15 '13 at 00:43