I was wondering what the intended purpose of the ByteArray
type was. The cryptography functionality seems to be using it. And in version 11.1 we have BinarySerialize
, which people are also a bit confused about (including myself, so consider that function included in this question as well).
The most straightforward guess about BinaryArray
is that it is a space-efficient and consistent way to store binary data. We could use a list of integers, but that is not space efficient (each takes at least 8 bytes) and the 0..255
range is not enforced.
If such a space efficient data type is to be useful, it should be possible to convert/transfer it without the overhead of an inefficient integer-list intermediate representation.
How can we convert/transfer ByteArray
to/from:
Files. Is there a function like BinaryReadList
to handle it?
LibraryLink. Can I transfer a byte array efficiently to C? Can I convert it to a byte-type RawArray (which is already supported by LibraryLink)?
Strings. String are sometimes used to represent the contents of files, or binary data, in a byte-perfect way. We have ImportSting
/ExportString
for this reason. Strings are not as good for this purpose as a real byte array because each character takes 2 bytes (and hopefully this will change in the future to allow for things beyond the basic multilingual plane in Unicode)
Base64 encoded data in strings. This is how ByteArrays show up in InputForm
, though the documentation suggests that they are stored more efficient internally. Such a string can be converted to a ByteArray
using Developer`DecodeBase64ToByteArray
. What about the reverse conversion?
It would also be nice to have an equivalent of StringToStream
for ByteArray
s.
If some of the above are not possible, please consider them a feature request. Regarding reading/writing from/to files, a lightweight function would be preferred (as opposed to the heavyweight, high overhead Import
/Export
which cannot even be used during initialization, i.e. in init.m
)
What can we do with ByteArray
s other than use them with the cryptography functions?
The documentation mentions that we can use Part
, First
, Last
, Min
, Max
.
By experimentation, Take
, Drop
, Length
, Dimensions
, Rest
, Most
also work.
So do BitAnd
, BitOr
, etc.
HTTPRequestData
and related functions support the property "BodyByteArray"
Is there anything else?