Sidebar

Home



Expressions V4


Tutorials

How-To

Reference

  Lexical basics
  Type system
  Variables and assignment
  Operators
  Expression rules
  Control flow
  Functions
  Built-in functions
   None and NaN
   Arithmetic
   Algebra
   Logarithmic and Exponential
   Trigonometric
   Rounding and Centering
   Strings
   Output, Formatting, and Errors
   Dialogs, Clipboard, Platform, and Paths
  Methods and properties
  Built-in methods
   Common Value Methods
   Number Methods
   String Basic Methods
   String Slice Methods
   String Parsing Methods
   String Formatting Methods
   String Regex Methods
   String Trim and Case Methods
  Objects
  Built-in objects
   Collections
    array
    map
    data_array
   Image data
   Image stream
   Dialogs
    fileopen
    filesave
   GUI objects
    window
    panel
    group
    splitpanel
    label
    textbutton
    drawablebutton
    togglebutton
    togglegroup
    textinput
    textedit
    numinput
    slider
    combobox
    listbox
    progressbar
    led
    separator
    menu
    image
    snake
  Classes and user-defined objects
  Include system
  Error model
  Execution model and sessions
  Host integration
  Limits and performance
  Formal reference
  Glossary

Cookbook

exprv4:reference:objects:data_array

data_array

`data_array` stores raw bytes. Use it when Expr code needs binary data, image raw pixels, base64 data, or packed numeric values.

A `data_array` is cloneable. `clone()` returns a separate copy of the byte buffer.


Constructor

data_array()

Creates an empty byte array.

data = data_array();
data.size();        // 0

data_array(size)

Creates a byte array with `size` bytes initialized to zero.

Arguments:

Argument Description
`size` Initial byte count. Negative values are treated as 0.
data = data_array(4);
data.to_hex();      // "00000000"

Size and State

Method Returns Description
`size()` number Number of bytes.
`count()` number Same as `size()`.
`empty()` boolean `true` when the array has no bytes.
`clear()` boolean Removes all bytes and returns `true`.
`resize(size)` number Resizes the buffer and returns the new size. New bytes are zero.
data = data_array();
data.resize(3);
print(data.size());

Raw Byte Access

get(index)

Returns the byte at `index` as a number. If `index` is outside the buffer, it returns `none()`.

data = data_array();
data.add(65);
print(data.get(0));     // 65

get(index, count)

Returns a new `data_array` containing up to `count` bytes starting at `index`. If the range is outside the buffer, an empty `data_array` is returned.

data = data_array();
data.add_string("ABCDEF");
part = data.get(1, 3);
print(part.get_string(0));   // BCD

set(index, value)

Writes one byte at `index`, or writes all bytes from another `data_array`. Writing past the end grows the buffer and fills gaps with zero bytes.

Arguments:

Argument Description
`index` Byte position. Negative values are treated as 0.
`value` Byte value, or another `data_array`.
data = data_array();
data.set(2, 255);
print(data.to_hex());        // 0000ff

add(value)

Appends one byte, or appends all bytes from another `data_array`.

data = data_array();
data.add(65);
data.add(66);
print(data.get_string(0));   // AB

insert(index) / insert(index, count)

Inserts zero bytes. With one argument it inserts one byte. With two arguments it inserts `count` zero bytes.

data = data_array();
data.add_string("AC");
data.insert(1);
data.set(1, 66);
print(data.get_string(0));   // ABC

erase(index) / erase(index, count)

Removes bytes. With one argument it removes one byte. With two arguments it removes up to `count` bytes.

data = data_array();
data.add_string("ABCD");
data.erase(1, 2);
print(data.get_string(0));   // AD

Strings

Method Returns Description
`get_string(index)` string Returns text from `index` to the end.
`get_string(index, count)` string Returns up to `count` bytes as text.
`set_string(index, text)` boolean Writes string bytes at `index`. Grows the buffer if needed.
`add_string(text)` boolean Appends string bytes.
data = data_array();
data.add_string("HELLO");
print(data.get_string(1, 3));    // ELL

Typed Numeric Access

Typed methods read, write, and append packed numeric values.

Type Read Write Append Bytes
signed 8-bit `get_s8(index)` `set_s8(index, value)` `add_s8(value)` 1
unsigned 8-bit `get_u8(index)` `set_u8(index, value)` `add_u8(value)` 1
signed 16-bit `get_s16(index[, littleEndian])` `set_s16(index, value[, littleEndian])` `add_s16(value[, littleEndian])` 2
unsigned 16-bit `get_u16(index[, littleEndian])` `set_u16(index, value[, littleEndian])` `add_u16(value[, littleEndian])` 2
signed 32-bit `get_s32(index[, littleEndian])` `set_s32(index, value[, littleEndian])` `add_s32(value[, littleEndian])` 4
signed 32-bit alias `get_s3(index[, littleEndian])` `set_s3(index, value[, littleEndian])` `add_s3(value[, littleEndian])` 4
unsigned 32-bit `get_u32(index[, littleEndian])` `set_u32(index, value[, littleEndian])` `add_u32(value[, littleEndian])` 4
float `get_float(index[, littleEndian])` `set_float(index, value[, littleEndian])` `add_float(value[, littleEndian])` 4
double `get_double(index[, littleEndian])` `set_double(index, value[, littleEndian])` `add_double(value[, littleEndian])` 8

`littleEndian` defaults to `true`. If a typed read does not have enough bytes, it returns `none()`.

data = data_array();
data.add_u16(4660);       // 0x1234, little-endian by default
print(data.to_hex());     // 3412
print(data.get_u16(0));   // 4660

to_hex()

Returns the buffer as lowercase hexadecimal text.

data = data_array();
data.add_string("A");
print(data.to_hex());     // 41

to_base64()

Returns the buffer encoded as base64.

data = data_array();
data.add_string("ABC");
encoded = data.to_base64();

from_base64(text)

Replaces the buffer with decoded base64 data and returns the decoded byte count.

data = data_array();
count = data.from_base64("QUJD");
print(data.get_string(0));    // ABC

find(value)

Searches for a byte, string, or another `data_array`. Returns the first byte index, `-1` when not found, or `none()` for unsupported argument types.

data = data_array();
data.add_string("ABCDEF");
print(data.find("CD"));       // 2
print(data.find(70));         // 5

Previous: map

Next: image_data

exprv4/reference/objects/data_array.txt · Last modified: by 127.0.0.1

Page Tools