"ByteArray" (Compiled Type)
"ByteArray"
represents a one-dimensional array of bytes.
Details
- "ByteArray" can be used in Typed and related constructs to specify a type.
- "ByteArray" objects are automatically memory managed.
Constructors
- ByteArray["string"] constructs a "ByteArray" object by extracting byte values from a Base64-encoded string.
- ByteArray[na] constructs a copy of the array na, where na has the type "NumericArray"::["UnsignedInteger8", 1].
- CreateTypeInstance["ByteArray",len] constructs a byte array of length len.
- CreateTypeInstance["ByteArray",byte,len] constructs a byte array of length len filled with byte values.
Conversions
- "ByteArray" objects can be converted to and from ByteArray expressions.
- "ByteArray" objects can be converted to and from "NumericArray"::["UnsignedInteger8",1] objects.
Expressions
"NumericArray"
Examples
open all close allBasic Examples (2)
"ByteArray" can be used as input or output for a CompiledCodeFunction:
cf = FunctionCompile[Function[Typed[ba, "ByteArray"], ba]]cf[ByteArray[{1, 2, 3}]]Create a "ByteArray" in compiled code:
func = FunctionCompile@Function[Typed[str, "String"],
ByteArray[str]
]func["AQIDBAUGBwg="]Scope (8)
Construction (4)
Construct a "ByteArray" from a string:
func = FunctionCompile@Function[Typed[str, "String"],
ByteArray[str]
]func["AQIDBAUGBwg="]Construct a "ByteArray" from a numeric array:
func = FunctionCompile@Function[Typed[na, "NumericArray"::["UnsignedInteger8", 1]],
ByteArray[na]
]func[NumericArray[{1, 2, 3}, "UnsignedInteger8"]]Create a "ByteArray" instance of a given length:
func = FunctionCompile@Function[Typed[len, "MachineInteger"],
CreateTypeInstance["ByteArray", len]
]Length[func[10]]Note that the created byte array is filled with random values:
Normal[func[10]]Create a "ByteArray" instance of a given length filled with an initial value:
func = FunctionCompile@Function[{Typed[byte, "UnsignedInteger8"], Typed[len, "MachineInteger"]},
CreateTypeInstance["ByteArray", byte, len]
]Normal[func[42, 10]]Conversion (4)
Convert a "ByteArray" to a "NumericArray":
func = FunctionCompile@Function[{Typed[ba, "ByteArray"]},
Cast[ba, "NumericArray"::["UnsignedInteger8", 1]]
]func[ByteArray[{1, 2, 3}]]Convert a "ByteArray" from a "NumericArray":
func = FunctionCompile@Function[{Typed[na, "NumericArray"::["UnsignedInteger8", 1]]},
Cast[na, "ByteArray"]
]func[NumericArray[{1, 2, 3}, "UnsignedInteger8"]]Convert a "ByteArray" to an "InertExpression":
func = FunctionCompile@Function[{Typed[ba, "ByteArray"]},
Cast[ba, "InertExpression"]
]func[ByteArray[{1, 2, 3}]]Convert a "ByteArray" from an "InertExpression":
func = FunctionCompile@Function[{Typed[expr, "InertExpression"]},
Cast[expr, "ByteArray"]
]func[ByteArray[{1, 2, 3}]]Properties & Relations (5)
func = FunctionCompile@Function[Typed[ba, "ByteArray"],
Length[ba]
]func[ByteArray[{1, 2, 3}]]func = FunctionCompile@Function[Typed[ba, "ByteArray"],
Head[ba]
]func[ByteArray[{1, 2, 3}]]func = FunctionCompile@Function[{Typed[ba, "ByteArray"], Typed[x, "MachineInteger"]},
ba[[x]]
]func[ByteArray[{1, 2, 3}], 2]Convert a "ByteArray" to a normal expression:
func = FunctionCompile@Function[{Typed[ba, "ByteArray"]},
Normal[ba]
]func[ByteArray[{1, 2, 3}]]Convert a "ByteArray" to a string:
func = FunctionCompile@Function[{Typed[ba, "ByteArray"]},
ToString[ba]
]func[ByteArray[{1, 2, 3}]]Possible Issues (2)
Part access can cause a runtime error if the element does not exist:
func = FunctionCompile[Function[Typed[ba, "ByteArray"], ba[[4]]]];
func[ByteArray[{1, 2, 3}]]Casting can cause a runtime error if the object cannot be converted to the target type:
func = FunctionCompile[Function[Typed[expr, "InertExpression"], Cast[expr, "ByteArray"]]];
func[4]Tech Notes
Related Guides
History
Introduced in 2023 (13.3)