Suppose I have a binary file which consists of a list of integer or real numbers. Let's say signed 8-bit integers; so each byte of the file is a single such integer. How can I read it and turn it into a NumericArray
with Integer8
? (without going through a List
, obviously).
I could not find anything like that in the documentation. The closest I found is to read the file as ByteArray
and then try to use what's suggested in the last answer of
How to convert a RawArray of bytes to ByteArray?
to convert to NumericArray
. Unfortunately this only seems to work with Byte
, i.e., unsigned 8-bit integers:
test = ReadByteArray["data/set0-config"];
test2 = NumericArray[test, "Integer8"]
fails with the unhelpful error message
NumericArray::nconvsa: The argument ByteArray[...] cannot be automatically converted to a NumericArray of type Integer8. Try using one of the following conversion methods: "Round", "ClipAndCoerce" or "ClipAndRound".
(none of these methods achieve the desired result, not to mention the fact that I don't want Mathematica to rewrite the whole binary data).
Overall, I've found very little information on NumericArray
(e.g., another obvious question is whether they can be used in compiled functions).
BinaryReadList
returns aList
, i.e. a representation that is neither efficient, nor one-to-one. OP wants a function which returns aNumericArray
corresponding to the type being read. $\endgroup$NumericArray[test, "UnsignedInteger8"]
fix things for you? $\endgroup$BinaryReadList
returns a packed array, not a list, and I thought that would be quite efficient. Granted, theByteCount
is about 8 bytes per number, so it's not one-to-one on a binary level; but for speed purposes it's pretty good. Otherwise,ByteArray@BinaryReadList[name, "Byte"]
? $\endgroup$List
, except for a few trivial kinds of manipulations like taking part of the array. $\endgroup$