Binary data
The data type Binary represents raw binary data as one-dimensional arrays of unsigned bytes. Examples of data that can be represented as byte arrays are raw data from sensors, raster images and compressed data.
Byte arrays can be constructed with the function binary(Charstring hex)->Binary
that converts a hexadecimal string hex
to a byte array and accessed using the usual array indexing notation b[i]
.
For example:
set :b1 = binary('010245FF')
To run this code block you must be logged in and your studio instance must be started.
:b1[2]
To run this code block you must be logged in and your studio instance must be started.
You can also convert a vector to a byte array with the function binary(Vector of Integer)->Binary
:
set :b2 = binary([0,1,255,48,35])
To run this code block you must be logged in and your studio instance must be started.
General queries over binary arrays can be specified. For example:
select i, :b1[i] from Integer i
To run this code block you must be logged in and your studio instance must be started.
select i,:b1[i]+1 from Integer i where i>2
To run this code block you must be logged in and your studio instance must be started.
You can update byte arrays with the function setf(Binary b, Integer i, Integer v)->Integer
:
setf(:b1, 2, 3)
To run this code block you must be logged in and your studio instance must be started.
You can use the in()
function and operator to obtain the elements of a byte array:
in(:b1);
To run this code block you must be logged in and your studio instance must be started.
The hexadecimal string representation of byte arrays and integers can be obtained with the function hex()
:
hex(:b1);
hex(binary(1234))
To run this code block you must be logged in and your studio instance must be started.
Functions
binary(Vector of Integer v)->Binary
Convert v
to byte array
binary(Charstring hex)->Binary
Convert hexadecimal string hex
to binary object
binary(Integer i)->Binary
Convert integer i
to binary object
bitreverse(Binary b)->Binary
Reverse bit order in binary object b
charstring(Binary b)->Charstring
Extract string of bytes from binary object b
dim(Binary b)->Integer
The number of bytes in binary object 'b'
dim(Memory m)->Integer
The number of bytes in byte array m
hex(Binary b)->Charstring
Convert binary object b
to hexadecimal string
hex2integer(Charstring hexnum)->Integer
Convert hexadecimal number hexnum
to corresponding integer
in(Binary b)->Bag of Integer
The elements of byte array b
in(Memory m)->Bag of Integer
The elements of byte array in m
integer(Binary b)->Integer
Convert binary object b
to integer
integer2hex(Integer i)->Charstring
Convert integer i
to the corresponding hexadecimal number
make_binary(Integer sz)->Binary
Construct a new binary object with 'sz' bytes
new_memory(Integer s)->Memory
Create new Memory object of size s
raw_file(Charstring path)->Memory
section(Binary b,Integer l,Integer u)->Binary
Elements from position l
to u
in binary object b
setf(Binary b,Integer i,Integer v)->Boolean
Set byte i
in binary object b
to v
setf(Memory m,Integer i,Integer v)->Boolean
Set byte i
in byte array in m
to v
skip(Binary b,Integer n)->Binary
Skip first n
elements in binary object b
s_bits(Integer u,Integer o,Integer l)->Integer
Get signed bit field of length 'l' at offset 'o' in 'i'
s_bits(Binary b,Integer o,Integer l)->Integer
Get signed bit field of length 'l' at offset 'o' in 'b'
s_bits(Memory m,Integer o,Integer l)->Integer
Get unsigned bit field of length 'l' at offset 'o' in 'm'
unpack(Binary b,Charstring frm)->Vector of Integer
Unpack binary object b
based on format frm
.
I32 -> Read the next 32 bits as a signed integer.
u16 -> Read the next 16 bits as an unsigned integer.
Z08 -> Skip the next 8 bits.
unpack(Charstring hex,Charstring frm)->Vector of Integer
Unpack hexadecimal string hex
based on format frm
.
frm
is a string with format specifications [IiUuZz][0-9][0-9]+
.
Examples:
I32 -> Read the next 32 bits as a signed integer.
u16 -> Read the next 16 bits as an unsigned integer.
Z08 -> Skip the next 8 bits.
u_bits(Integer u,Integer o,Integer l)->Integer
Get unsigned bit field of length 'l' at offset 'o' in 'i'
u_bits(Binary b,Integer o,Integer l)->Integer
Get unsigned bit field of length 'l' at offset 'o' in 'b'
u_bits(Memory m,Integer o,Integer l)->Integer
Get unsigned bit field of length 'l' at offset 'o' in 'm'
vref(Binary b,Integer i)->Integer v
Same as b[i]
to get byte i
in binary object b
vref(Memory m,Integer i)->Integer
Same as m[i]
vref(Tuple t,Number i)->Object