Skip to content
Stand with Ukraine flag

TBEL Helper Functions

TBEL provides a rich set of built-in helper functions for encoding, byte manipulation, hex conversion, date/time, and geofencing. For language syntax and data types, see the Language Guide.

Creates a Base64-encoded ASCII string from a binary string.

ParameterTypeDescription
inputStringThe binary string to encode

Returns: String — Base64-encoded string.

var encodedData = btoa("Hello, world"); // encode a string
var decodedData = atob(encodedData); // decode the string

Decodes a Base64-encoded string back to a binary string.

ParameterTypeDescription
inputStringBase64-encoded string

Returns: String — decoded binary string.

var encodedData = btoa("Hello, world");
var decodedData = atob(encodedData); // "Hello, world"

Rounds a double value towards the nearest neighbor with the given precision.

ParameterTypeDescription
valuedoubleThe value to round
precisionintNumber of decimal places

Returns: double — rounded value.

toFixed(0.345, 1); // 0.3
toFixed(0.345, 2); // 0.35

Rounds a double value to the nearest integer.

ParameterTypeDescription
valuedoubleThe value to round

Returns: int — rounded integer.

toInt(0.3); // 0
toInt(0.5); // 1
toInt(2.7); // 3

Converts a string to a list of bytes.

ParameterTypeDescription
inputStringThe string to convert
charsetNameStringOptional charset (default: UTF-8)

Returns: List — list of byte values.

stringToBytes("hello world");
// [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
var base64Str = "eyJoZWxsbyI6ICJ3b3JsZCJ9";
var bytesStr = atob(base64Str);
stringToBytes(bytesStr);
// [123, 34, 104, 101, 108, 108, 111, 34, 58, 32, 34, 119, 111, 114, 108, 100, 34, 125]
stringToBytes("hello world", "UTF8");
// [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

Creates a string from a list of bytes.

ParameterTypeDescription
bytesListListA list of byte values
charsetNameStringOptional charset (default: UTF-8)

Returns: String — decoded string.

var bytes = [0x48, 0x45, 0x4C, 0x4C, 0x4F];
bytesToString(bytes); // "HELLO"

Alias for bytesToString.

Decodes a list of bytes to a JSON document.

ParameterTypeDescription
bytesListListA list of byte values

Returns: JSON object or primitive.

var base64Str = "eyJoZWxsbyI6ICJ3b3JsZCJ9";
var bytesStr = atob(base64Str);
var bytes = stringToBytes(bytesStr);
decodeToJson(bytes); // {"hello": "world"}

Checks whether a string represents a binary (base-2) value.

ParameterTypeDescription
strStringThe string to check

Returns: int2 if binary, -1 otherwise.

isBinary("1100110"); // 2
isBinary("2100110"); // -1

Checks whether a string represents an octal (base-8) value.

ParameterTypeDescription
strStringThe string to check

Returns: int8 if octal, -1 otherwise.

isOctal("4567734"); // 8
isOctal("8100110"); // -1

Checks whether a string represents a decimal (base-10) value.

ParameterTypeDescription
strStringThe string to check

Returns: int10 if decimal, -1 otherwise.

isDecimal("4567039"); // 10
isDecimal("C100110"); // -1

Checks whether a string represents a hexadecimal (base-16) value.

ParameterTypeDescription
strStringThe string to check

Returns: int16 if hexadecimal, -1 otherwise.

isHexadecimal("F5D7039"); // 16
isHexadecimal("K100110"); // -1

Checks whether a double value is Not-a-Number.

ParameterTypeDescription
valuedoubleThe value to check

Returns: booleantrue if NaN.

isNaN(0.0 / 0.0); // true
isNaN(1.0); // false

Encodes a URI string, preserving reserved characters: A–Z a–z 0–9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , #

ParameterTypeDescription
uriStringThe URI to encode

Returns: String — encoded URI.

var uri = "-_.!~*'();/?:@&=+$,#ht://example.ж д a/path with spaces/?param1=Київ 1&param2=Україна2";
encodeURI(uri);
// "-_.!~*'();/?:@&=+$,#ht://example.%D0%B6%20%D0%B4%20a/path%20with%20spaces/?param1=%D0%9A%D0%B8%D1%97%D0%B2%201&param2=%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D0%B02"

Decodes a URI previously encoded with encodeURI.

ParameterTypeDescription
uriStringThe encoded URI

Returns: String — decoded URI.

var encoded = "-_.!~*'();/?:@&=+$,#ht://example.%D0%B6%20%D0%B4%20a/path%20with%20spaces/?param1=%D0%9A%D0%B8%D1%97%D0%B2%201&param2=%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D0%B02";
decodeURI(encoded);
// "-_.!~*'();/?:@&=+$,#ht://example.ж д a/path with spaces/?param1=Київ 1&param2=Україна2"

Throws a RuntimeException with the given message.

ParameterTypeDescription
messageStringException message
raiseError("frequency_weighting_type must be 0, 1 or 2.");
// throws RuntimeException: "frequency_weighting_type must be 0, 1 or 2."
var value = 4;
var message = "frequency_weighting_type must be 0, 1 or 2. A value of " + value + " is invalid.";
raiseError(message);
// throws RuntimeException with the composed message

Converts a list of signed bytes to their unsigned integer representation.

ParameterTypeDescription
byteArrayListList of signed byte values

Returns: List — unsigned byte values (0–255).

var list = hexToBytes("D8FF");
printUnsignedBytes(list); // [216, 255]
var list2 = hexToBytes("FFD8");
printUnsignedBytes(list2); // [255, 216]

Pads a string from the start until the target length is reached.

ParameterTypeDescription
strStringThe string to pad
targetLengthintDesired result length
padStringcharCharacter to pad with

Returns: String — padded string. If the string is already at or beyond the target length, it is returned unchanged.

padStart("010011", 8, '0'); // "00010011"
padStart("1001010011", 8, '0'); // "1001010011" (already >= 8)
padStart("1001010011", 16, '*'); // "******1001010011"
var fullNumber = "203439900FFCD5581";
var last6 = fullNumber.substring(11);
padStart(last6, fullNumber.length(), '*'); // "***********CD5581"

Pads a string from the end until the target length is reached.

ParameterTypeDescription
strStringThe string to pad
targetLengthintDesired result length
padStringcharCharacter to pad with

Returns: String — padded string. If the string is already at or beyond the target length, it is returned unchanged.

padEnd("010011", 8, '0'); // "01001100"
padEnd("1001010011", 8, '0'); // "1001010011" (already >= 8)
padEnd("1001010011", 16, '*'); // "1001010011******"
var fullNumber = "203439900FFCD5581";
var first11 = fullNumber.substring(0, 11);
padEnd(first11, fullNumber.length(), '*'); // "203439900FF******"

Converts an integer to a hexadecimal string.

ParameterTypeDescription
iIntegerThe integer to convert
bigEndianbooleanOptional. Big-endian byte order (default: true)
prefbooleanOptional. Include 0x prefix (default: false)
lenintOptional. Number of bytes to include

Returns: String — hex string.

intToHex(0x7FFFFFFF, true, true); // "0x7FFFFFFF"
intToHex(171, true, false); // "AB"
intToHex(0xABCDEF, false, true, 4); // "0xCDAB"
intToHex(0xABCD, false, false, 2); // "AB"

Converts a long to a hexadecimal string.

ParameterTypeDescription
lLongThe long to convert
bigEndianbooleanOptional. Big-endian byte order (default: true)
prefbooleanOptional. Include 0x prefix (default: false)
lenintOptional. Number of bytes to include

Returns: String — hex string.

longToHex(9223372036854775807, true, true); // "0x7FFFFFFFFFFFFFFF"
longToHex(0x7A12BCD3, true, true, 4); // "0xBCD3"
longToHex(0x7A12BCD3, false, false, 4); // "127A"

Converts a float to a hexadecimal string using IEEE 754 representation.

ParameterTypeDescription
fFloatThe float to convert
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: String — hex string with 0x prefix.

floatToHex(123456789.00); // "0x4CEB79A3"
floatToHex(123456789.00, false); // "0xA379EB4C"

Converts a double to a hexadecimal string using IEEE 754 representation.

ParameterTypeDescription
dDoubleThe double to convert
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: String — hex string with 0x prefix.

doubleToHex(1729.1729d); // "0x409B04B10CB295EA"
doubleToHex(1729.1729d, false); // "0xEA95B20CB1049B40"

Converts a number to a string in the specified radix (binary, octal, decimal, or hex).

ParameterTypeDescription
numberLongThe number to convert
radixintOptional. Base: 2, 8, 10, or 16 (default: 10)
bigEndianbooleanOptional. Big-endian byte order (default: true)
prefbooleanOptional. Include 0x prefix for hex (default: false)

Returns: String — the number in the specified radix.

intLongToRadixString(58, 2); // "00111010"
intLongToRadixString(9223372036854775807, 2); // "0111111111...1" (63 digits)
intLongToRadixString(13158, 8); // "31546"
intLongToRadixString(-13158, 8); // "1777777777777777746232"
intLongToRadixString(-13158, 10); // "-13158"
intLongToRadixString(13158, 16); // "3366"
intLongToRadixString(-13158, 16); // "FFCC9A"
intLongToRadixString(9223372036854775807, 16); // "7FFFFFFFFFFFFFFF"
intLongToRadixString(-13158, 16, true, true); // "0xFFCC9A"

Parses a hexadecimal string to an integer.

ParameterTypeDescription
hexStringHexadecimal string
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: int — parsed integer.

parseHexToInt("BBAA"); // 48042
parseHexToInt("BBAA", true); // 48042
parseHexToInt("AABB", false); // 48042
parseHexToInt("BBAA", false); // 43707

Aliases:

  • parseLittleEndianHexToInt(hex) — equivalent to parseHexToInt(hex, false)
  • parseBigEndianHexToInt(hex) — equivalent to parseHexToInt(hex, true)

Parses a hexadecimal string to a float (IEEE 754).

ParameterTypeDescription
hexStringHexadecimal string
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: float — parsed float.

parseHexToFloat("41EA62CC"); // 29.29824
parseHexToFloat("41EA62CC", true); // 29.29824
parseHexToFloat("41EA62CC", false); // -5.948442E7
parseHexToFloat("CC62EA41", false); // 29.29824

Aliases:

  • parseLittleEndianHexToFloat(hex) — equivalent to parseHexToFloat(hex, false)
  • parseBigEndianHexToFloat(hex) — equivalent to parseHexToFloat(hex, true)

Interprets a hex string as a plain integer value and returns it as a float. Unlike parseHexToFloat, this does not use IEEE 754 bit interpretation.

ParameterTypeDescription
hexStringHexadecimal string (may include 0x prefix)
bigEndianbooleanBig-endian byte order

Returns: float — the integer value as a float.

parseHexIntLongToFloat("0x0A", true); // 10.0
parseHexIntLongToFloat("0x0A", false); // 10.0
parseHexIntLongToFloat("0x00000A", true); // 10.0
parseHexIntLongToFloat("0x0A0000", false); // 10.0
parseHexIntLongToFloat("0x000A0A", true); // 2570.0
parseHexIntLongToFloat("0x0A0A00", false); // 2570.0

Parses a hexadecimal string to a double (IEEE 754).

ParameterTypeDescription
hexStringHexadecimal string
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: double — parsed double.

parseHexToDouble("409B04B10CB295EA"); // 1729.1729
parseHexToDouble("409B04B10CB295EA", true); // 1729.1729
parseHexToDouble("409B04B10CB295EA", false); // -2.7208640774822924E205
parseHexToDouble("EA95B20CB1049B40", false); // 1729.1729

Aliases:

  • parseLittleEndianHexToDouble(hex) — equivalent to parseHexToDouble(hex, false)
  • parseBigEndianHexToDouble(hex) — equivalent to parseHexToDouble(hex, true)

Converts a hexadecimal string to a list of byte values.

ParameterTypeDescription
hexStringHexadecimal string

Returns: List — list of signed byte values (-128 to 127).

hexToBytes("D8FF"); // [-40, -1]

Converts a hexadecimal string to a byte array.

ParameterTypeDescription
hexStringHexadecimal string

Returns: int[] — byte array.

Converts a list of bytes to a hexadecimal string.

ParameterTypeDescription
bytesListListList of byte values

Returns: String — hexadecimal string.

Parses a list of bytes to an integer.

ParameterTypeDescription
bytesListListList of byte values
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: int — parsed integer.

parseBytesToInt([0xBB, 0xAA]); // 48042
parseBytesToInt([0xBB, 0xAA], true); // 48042
parseBytesToInt([0xAA, 0xBB], false); // 48042

Parses a list of bytes to a long.

ParameterTypeDescription
bytesListListList of byte values
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: long — parsed long.

Parses a list of bytes to a float (IEEE 754).

ParameterTypeDescription
bytesListListList of byte values
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: float — parsed float.

Interprets a list of bytes as an integer and returns it as a float.

ParameterTypeDescription
bytesListListList of byte values
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: float — the integer value as a float.

Interprets a list of bytes as a long and returns it as a double.

ParameterTypeDescription
bytesListListList of byte values
bigEndianbooleanOptional. Big-endian byte order (default: true)

Returns: double — the long value as a double.

Converts a list of bytes to an ExecutionArrayList (TBEL’s internal list type).

ParameterTypeDescription
bytesListListList of byte values

Returns: List — execution array list.

Converts a single byte to an 8-element binary array.

ParameterTypeDescription
bintA single byte value

Returns: int[] — array of 8 binary digits (0 or 1).

parseByteToBinaryArray(0xFF); // [1, 1, 1, 1, 1, 1, 1, 1]
parseByteToBinaryArray(0x00); // [0, 0, 0, 0, 0, 0, 0, 0]

Converts a list of bytes to a binary array.

ParameterTypeDescription
bytesListListList of byte values

Returns: int[] — binary array (8 bits per byte).

parseBytesToBinaryArray([0xFF, 0x00]);
// [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Converts a long to a 64-element binary array.

ParameterTypeDescription
valuelongThe long value

Returns: int[] — array of 64 binary digits.

parseLongToBinaryArray(255L);
// [0,0,...,0, 1,1,1,1,1,1,1,1] (56 zeros followed by 8 ones)

Converts a binary array to an integer.

ParameterTypeDescription
binaryArrayint[]Array of binary digits (0s and 1s)

Returns: int — parsed integer.

parseBinaryArrayToInt([1, 1, 1, 1, 1, 1, 1, 1]); // 255
parseBinaryArrayToInt([0, 0, 1, 1, 1, 0, 1, 0]); // 58

Parses a string to an integer.

ParameterTypeDescription
strStringThe string to parse

Returns: int — parsed integer.

parseInt("123"); // 123

Parses a string to a long.

ParameterTypeDescription
strStringThe string to parse

Returns: long — parsed long.

parseLong("9223372036854775807"); // 9223372036854775807

Parses a string to a float.

ParameterTypeDescription
strStringThe string to parse

Returns: float — parsed float.

parseFloat("3.14"); // 3.14

Parses a string to a double.

ParameterTypeDescription
strStringThe string to parse

Returns: double — parsed double.

parseDouble("3.141592653589793"); // 3.141592653589793

TBEL supports standard bitwise operators inherited from Java:

OperatorDescriptionExample
&AND0xFF & 0x0F0x0F
|OR0xF0 | 0x0F0xFF
^XOR0xFF ^ 0x0F0xF0
~NOT (complement)~0x00-1
<<Left shift1 << 8256
>>Signed right shift-256 >> 4-16
>>>Unsigned right shift-1 >>> 2815
var a = 0xFF;
var b = 0x0F;
var andResult = a & b; // 15 (0x0F)
var orResult = a | b; // 255 (0xFF)
var xorResult = a ^ b; // 240 (0xF0)
var shifted = 1 << 8; // 256

Converts a Base64-encoded string to a hexadecimal string.

ParameterTypeDescription
base64StrStringBase64-encoded string

Returns: String — hexadecimal string.

base64ToHex("SGVsbG8gV29ybGQ="); // "48656C6C6F20576F726C64"

Converts a list of bytes to a Base64 string.

ParameterTypeDescription
bytesListListList of byte values

Returns: String — Base64-encoded string.

bytesToBase64([72, 101, 108, 108, 111]); // "SGVsbG8="

Converts a Base64 string to a list of bytes.

ParameterTypeDescription
base64StrStringBase64-encoded string

Returns: List — list of byte values.

base64ToBytes("SGVsbG8gV29ybGQ=");
// [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

Alias for base64ToBytes.

Flattens a nested JSON structure into a single-level map where nested keys are joined with dots.

Overload 1 — basic flattening:

var json = {"temperature": 42, "nested": {"humidity": 73}};
toFlatMap(json);
// {"temperature": 42, "nested.humidity": 73}

Overload 2 — with pathInKey flag:

ParameterTypeDescription
jsonObjectJSON object or string
pathInKeybooleantrue to include full path in keys, false to use only the leaf key
var json = {"temperature": 42, "nested": {"humidity": 73}};
toFlatMap(json, true); // {"temperature": 42, "nested.humidity": 73}
toFlatMap(json, false); // {"temperature": 42, "humidity": 73}

Overload 3 — with exclusion list:

ParameterTypeDescription
jsonObjectJSON object or string
excludeListListPaths to exclude from flattening
var json = {"temperature": 42, "nested": {"humidity": 73, "pressure": 1013}};
toFlatMap(json, ["pressure"]);
// {"temperature": 42, "nested.humidity": 73}

Overload 4 — with exclusion list and pathInKey:

ParameterTypeDescription
jsonObjectJSON object or string
excludeListListPaths to exclude from flattening
pathInKeybooleantrue to include full path in keys
var json = {"temperature": 42, "nested": {"humidity": 73, "pressure": 1013}};
toFlatMap(json, ["pressure"], true);
// {"temperature": 42, "nested.humidity": 73}

The Date class provides date/time creation and arithmetic.

var date1 = new Date("2024-01-15");
var date2 = new Date("15/01/2024", "dd/MM/yyyy");
var date3 = new Date("15/01/2024", "dd/MM/yyyy", "en_US");
var date4 = new Date("15/01/2024", "dd/MM/yyyy", "en_US", "UTC");
ParameterTypeDescription
dateStringStringThe date string to parse
patternStringOptional. Date format pattern (e.g. dd/MM/yyyy)
localeStringOptional. Locale (e.g. en_US, fr_FR)
timeZoneStringOptional. Time zone (e.g. UTC, America/New_York)
var date1 = new Date(2024, 1, 15);
var date2 = new Date(2024, 1, 15, 14, 30, 45);
var date3 = new Date(2024, 1, 15, "UTC");
var date4 = new Date(2024, 1, 15, 14, 30, 45, "UTC");
var options = {
pattern: "yyyy-MM-dd HH:mm:ss",
locale: "en_US",
timeZone: "UTC"
};
var date = new Date("2024-01-15 14:30:45", options);

Each method modifies the date in-place and returns the modified date:

var date = new Date(2024, 1, 15);
date.addYears(1); // 2025-01-15
date.addMonths(2); // 2025-03-15
date.addWeeks(1); // 2025-03-22
date.addDays(5); // 2025-03-27
date.addHours(3); // 2025-03-27 03:00
date.addMinutes(30); // 2025-03-27 03:30
date.addSeconds(45); // 2025-03-27 03:30:45
date.addMilliseconds(500); // 2025-03-27 03:30:45.500

Checks whether a geographic point lies inside a polygon boundary.

ParameterTypeDescription
latitudedoublePoint latitude
longitudedoublePoint longitude
polygonListList of [latitude, longitude] pairs defining the polygon vertices

Returns: booleantrue if the point is inside the polygon.

var polygon = [
[55.75, 37.61],
[55.75, 37.62],
[55.74, 37.62],
[55.74, 37.61]
];
isInsidePolygon(55.745, 37.615, polygon); // true

Checks whether a geographic point lies inside a circle.

ParameterTypeDescription
latitudedoublePoint latitude
longitudedoublePoint longitude
centerLatitudedoubleCircle center latitude
centerLongitudedoubleCircle center longitude
radiusInKmdoubleCircle radius in kilometers

Returns: booleantrue if the point is inside the circle.

isInsideCircle(55.745, 37.615, 55.75, 37.62, 1.5); // true (within 1.5 km)