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.
Encoding
Section titled “Encoding”Creates a Base64-encoded ASCII string from a binary string.
btoa(input)| Parameter | Type | Description |
|---|---|---|
input |
String |
The binary string to encode |
Returns: String — Base64-encoded string.
Example:
btoa("Hello, world"); // "SGVsbG8sIHdvcmxk"Decodes a Base64-encoded string back to a binary string.
atob(input)| Parameter | Type | Description |
|---|---|---|
input |
String |
Base64-encoded string |
Returns: String — decoded binary string.
Example:
atob("SGVsbG8sIHdvcmxk"); // "Hello, world"Number Rounding
Section titled “Number Rounding”toFixed
Section titled “toFixed”Rounds a double value towards the nearest neighbor with the given precision.
toFixed(value, precision)| Parameter | Type | Description |
|---|---|---|
value |
double |
The value to round |
precision |
int |
Number of decimal places |
Returns: double — rounded value.
Examples:
toFixed(0.345, 1); // 0.3toFixed(0.345, 2); // 0.35Rounds a double value to the nearest integer. Halves are rounded away from zero.
toInt(value)| Parameter | Type | Description |
|---|---|---|
value |
double |
The value to round |
Returns: int — rounded integer.
Examples:
toInt(0.3); // 0toInt(0.5); // 1toInt(2.7); // 3toInt(-2.5); // -3String and Byte Conversion
Section titled “String and Byte Conversion”stringToBytes
Section titled “stringToBytes”Converts a string to a list of bytes.
stringToBytes(input[, charsetName])| Parameter | Type | Description |
|---|---|---|
input |
String |
The string to convert |
charsetName |
String |
Optional charset (default: UTF-8) |
Returns: List — list of byte values.
Examples:
var str = "hello world";stringToBytes(str); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]stringToBytes(str, "UTF8"); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]To get the bytes of a Base64-encoded payload, decode it with atob first:
var bytesStr = atob("eyJoZWxsbyI6ICJ3b3JsZCJ9");stringToBytes(bytesStr);// [123, 34, 104, 101, 108, 108, 111, 34, 58, 32, 34, 119, 111, 114, 108, 100, 34, 125]bytesToString
Section titled “bytesToString”Creates a string from a list of bytes.
bytesToString(bytesList[, charsetName])| Parameter | Type | Description |
|---|---|---|
bytesList |
List |
A list of byte values |
charsetName |
String |
Optional charset (default: UTF-8) |
Returns: String — decoded string.
Example:
var bytes = [0x48, 0x45, 0x4C, 0x4C, 0x4F];bytesToString(bytes); // "HELLO"decodeToString
Section titled “decodeToString”Same as bytesToString, including the optional charsetName parameter.
decodeToString(bytesList[, charsetName])decodeToJson
Section titled “decodeToJson”Decodes a JSON string, or a list of bytes that contains one, to a JSON document.
decodeToJson(data)| Parameter | Type | Description |
|---|---|---|
data |
String or List |
A JSON string or a list of byte values |
Returns: JSON object or primitive.
Example:
decodeToJson('{"hello": "world"}'); // {"hello": "world"}To decode a JSON payload that arrives as bytes, pass the list of bytes:
var bytes = stringToBytes(atob("eyJoZWxsbyI6ICJ3b3JsZCJ9"));decodeToJson(bytes); // {"hello": "world"}Type Validation
Section titled “Type Validation”isBinary
Section titled “isBinary”Checks whether a string represents a binary (base-2) value.
isBinary(str)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to check |
Returns: int — 2 if binary, -1 otherwise.
Examples:
isBinary("1100110"); // 2isBinary("2100110"); // -1isOctal
Section titled “isOctal”Checks whether a string represents an octal (base-8) value.
isOctal(str)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to check |
Returns: int — 8 if octal, -1 otherwise.
Examples:
isOctal("4567734"); // 8isOctal("8100110"); // -1isDecimal
Section titled “isDecimal”Checks whether a string represents a decimal (base-10) value.
isDecimal(str)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to check |
Returns: int — 10 if decimal, -1 otherwise.
Examples:
isDecimal("4567039"); // 10isDecimal("C100110"); // -1isHexadecimal
Section titled “isHexadecimal”Checks whether a string represents a hexadecimal (base-16) value.
isHexadecimal(str)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to check |
Returns: int — 16 if hexadecimal, -1 otherwise.
Examples:
isHexadecimal("F5D7039"); // 16isHexadecimal("K100110"); // -1Checks whether a double value is Not-a-Number.
isNaN(value)| Parameter | Type | Description |
|---|---|---|
value |
double |
The value to check |
Returns: boolean — true if NaN.
Examples:
isNaN(0.0 / 0.0); // trueisNaN(1.0); // falseURI Encoding
Section titled “URI Encoding”encodeURI
Section titled “encodeURI”Encodes a URI string, preserving reserved characters: A–Z a–z 0–9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , #
encodeURI(uri)| Parameter | Type | Description |
|---|---|---|
uri |
String |
The URI to encode |
Returns: String — encoded URI.
Example:
var uri = "-_.!~*'();/?:@&=+$,#ht://example.ж д a/path with spaces/?param1=Київ 1¶m2=Україна2";encodeURI(uri);// "-_.!~*'();/?:@&=+$,#ht://example.%D0%B6%20%D0%B4%20a/path%20with%20spaces/?param1=%D0%9A%D0%B8%D1%97%D0%B2%201¶m2=%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D0%B02"decodeURI
Section titled “decodeURI”Decodes a URI previously encoded with encodeURI.
decodeURI(uri)| Parameter | Type | Description |
|---|---|---|
uri |
String |
The encoded URI |
Returns: String — decoded URI.
Example:
var encoded = "-_.!~*'();/?:@&=+$,#ht://example.%D0%B6%20%D0%B4%20a/path%20with%20spaces/?param1=%D0%9A%D0%B8%D1%97%D0%B2%201¶m2=%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D0%B02";decodeURI(encoded);// "-_.!~*'();/?:@&=+$,#ht://example.ж д a/path with spaces/?param1=Київ 1¶m2=Україна2"Utility
Section titled “Utility”raiseError
Section titled “raiseError”Throws a RuntimeException with the given message.
raiseError(message)| Parameter | Type | Description |
|---|---|---|
message |
String |
Exception message |
Example:
var value = 4;if (value > 2) { raiseError("frequency_weighting_type must be 0, 1 or 2. A value of " + value + " is invalid.");}// throws RuntimeException: "frequency_weighting_type must be 0, 1 or 2. A value of 4 is invalid."printUnsignedBytes
Section titled “printUnsignedBytes”Converts a list of signed bytes to their unsigned integer representation.
printUnsignedBytes(byteArray)| Parameter | Type | Description |
|---|---|---|
byteArray |
List |
List of signed byte values |
Returns: List — unsigned byte values (0–255).
Examples:
var list = hexToBytes("D8FF");printUnsignedBytes(list); // [216, 255]
var list2 = hexToBytes("FFD8");printUnsignedBytes(list2); // [255, 216]String Padding
Section titled “String Padding”padStart
Section titled “padStart”Pads a string from the start until the target length is reached.
padStart(str, targetLength, padString)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to pad |
targetLength |
int |
Desired result length |
padString |
char |
Character to pad with |
Returns: String — padded string. If the string is already at or beyond the target length, it is returned unchanged.
Examples:
padStart("010011", 8, '0'); // "00010011"padStart("1001010011", 8, '0'); // "1001010011" (already >= 8)padStart("1001010011", 16, '*'); // "******1001010011"To mask a value and keep only its last characters:
var fullNumber = "203439900FFCD5581";var last6 = fullNumber.substring(11);padStart(last6, fullNumber.length(), '*'); // "***********CD5581"padEnd
Section titled “padEnd”Pads a string from the end until the target length is reached.
padEnd(str, targetLength, padString)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to pad |
targetLength |
int |
Desired result length |
padString |
char |
Character to pad with |
Returns: String — padded string. If the string is already at or beyond the target length, it is returned unchanged.
Examples:
padEnd("010011", 8, '0'); // "01001100"padEnd("1001010011", 8, '0'); // "1001010011" (already >= 8)padEnd("1001010011", 16, '*'); // "1001010011******"To mask a value and keep only its first characters:
var fullNumber = "203439900FFCD5581";var first11 = fullNumber.substring(0, 11);padEnd(first11, fullNumber.length(), '*'); // "203439900FF******"Number-to-Hex Conversion
Section titled “Number-to-Hex Conversion”intToHex
Section titled “intToHex”Converts an integer to a hexadecimal string.
intToHex(i[, bigEndian[, pref[, len]]])| Parameter | Type | Description |
|---|---|---|
i |
Integer |
The integer to convert |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
pref |
boolean |
Optional. Include 0x prefix (default: false) |
len |
int |
Optional. Number of hex digits to keep, counted from the end. Must not be greater than the length of the hex string, otherwise the call fails. |
Returns: String — hex string.
Examples:
intToHex(0x7FFFFFFF, true, true); // "0x7FFFFFFF"intToHex(171, true, false); // "AB"intToHex(0xABCDEF, false, true, 4); // "0xCDAB"intToHex(0xABCD, false, false, 2); // "AB"longToHex
Section titled “longToHex”Converts a long to a hexadecimal string.
longToHex(l[, bigEndian[, pref[, len]]])| Parameter | Type | Description |
|---|---|---|
l |
Long |
The long to convert |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
pref |
boolean |
Optional. Include 0x prefix (default: false) |
len |
int |
Optional. Number of hex digits to keep, counted from the end. Must not be greater than the length of the hex string, otherwise the call fails. |
Returns: String — hex string.
Examples:
longToHex(9223372036854775807, true, true); // "0x7FFFFFFFFFFFFFFF"longToHex(0x7A12BCD3, true, true, 4); // "0xBCD3"longToHex(0x7A12BCD3, false, false, 4); // "127A"floatToHex
Section titled “floatToHex”Converts a float to a hexadecimal string using IEEE 754 representation.
floatToHex(f[, bigEndian])| Parameter | Type | Description |
|---|---|---|
f |
Float |
The float to convert |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: String — hex string with 0x prefix.
Examples:
floatToHex(123456789.00); // "0x4CEB79A3"floatToHex(123456789.00, false); // "0xA379EB4C"doubleToHex
Section titled “doubleToHex”Converts a double to a hexadecimal string using IEEE 754 representation.
doubleToHex(d[, bigEndian])| Parameter | Type | Description |
|---|---|---|
d |
Double |
The double to convert |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: String — hex string with 0x prefix.
Examples:
doubleToHex(1729.1729d); // "0x409B04B10CB295EA"doubleToHex(1729.1729d, false); // "0xEA95B20CB1049B40"intLongToRadixString
Section titled “intLongToRadixString”Converts a number to a string in the specified radix (binary, octal, decimal, or hex).
intLongToRadixString(number[, radix[, bigEndian[, pref]]])| Parameter | Type | Description |
|---|---|---|
number |
Long |
The number to convert |
radix |
int |
Optional. Base: 2, 8, 10, 16, or from 25 to 36 (default: 10) |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
pref |
boolean |
Optional. Include 0x prefix for hex (default: false) |
Returns: String — the number in the specified radix.
Examples:
intLongToRadixString(58, 2); // "00111010"intLongToRadixString(9223372036854775807, 2); // "0111...1" (64 digits: 0 followed by 63 ones)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"intLongToRadixString(255, 36); // "73"Hex String Parsing
Section titled “Hex String Parsing”parseHexToInt
Section titled “parseHexToInt”Parses a hexadecimal string to an integer.
parseHexToInt(hex[, bigEndian])| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: int — parsed integer.
Examples:
parseHexToInt("BBAA"); // 48042parseHexToInt("BBAA", true); // 48042parseHexToInt("AABB", false); // 48042parseHexToInt("BBAA", false); // 43707Aliases:
parseLittleEndianHexToInt(hex)— equivalent toparseHexToInt(hex, false)parseBigEndianHexToInt(hex)— equivalent toparseHexToInt(hex, true)
parseHexToLong
Section titled “parseHexToLong”Parses a hexadecimal string to a long.
parseHexToLong(hex[, bigEndian])| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: long — parsed long.
Examples:
parseHexToLong("BBAA"); // 48042parseHexToLong("AABB", false); // 48042Aliases:
parseLittleEndianHexToLong(hex)— equivalent toparseHexToLong(hex, false)parseBigEndianHexToLong(hex)— equivalent toparseHexToLong(hex, true)
parseHexToFloat
Section titled “parseHexToFloat”Parses a hexadecimal string to a float (IEEE 754).
parseHexToFloat(hex[, bigEndian])| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: float — parsed float.
Examples:
parseHexToFloat("41EA62CC"); // 29.29824parseHexToFloat("41EA62CC", true); // 29.29824parseHexToFloat("41EA62CC", false); // -5.948442E7parseHexToFloat("CC62EA41", false); // 29.29824Aliases:
parseLittleEndianHexToFloat(hex)— equivalent toparseHexToFloat(hex, false)parseBigEndianHexToFloat(hex)— equivalent toparseHexToFloat(hex, true)
parseHexIntLongToFloat
Section titled “parseHexIntLongToFloat”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.
parseHexIntLongToFloat(hex, bigEndian)| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string (may include 0x prefix) |
bigEndian |
boolean |
Big-endian byte order |
Returns: float — the integer value as a float.
Examples:
parseHexIntLongToFloat("0x0A", true); // 10.0parseHexIntLongToFloat("0x0A", false); // 10.0parseHexIntLongToFloat("0x00000A", true); // 10.0parseHexIntLongToFloat("0x0A0000", false); // 10.0parseHexIntLongToFloat("0x000A0A", true); // 2570.0parseHexIntLongToFloat("0x0A0A00", false); // 2570.0parseHexToDouble
Section titled “parseHexToDouble”Parses a hexadecimal string to a double (IEEE 754).
parseHexToDouble(hex[, bigEndian])| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true) |
Returns: double — parsed double.
Examples:
parseHexToDouble("409B04B10CB295EA"); // 1729.1729parseHexToDouble("409B04B10CB295EA", true); // 1729.1729parseHexToDouble("409B04B10CB295EA", false); // -2.7208640774822924E205parseHexToDouble("EA95B20CB1049B40", false); // 1729.1729Aliases:
parseLittleEndianHexToDouble(hex)— equivalent toparseHexToDouble(hex, false)parseBigEndianHexToDouble(hex)— equivalent toparseHexToDouble(hex, true)
Hex and Byte Conversion
Section titled “Hex and Byte Conversion”hexToBytes
Section titled “hexToBytes”Converts a hexadecimal string to a list of byte values.
hexToBytes(hex)| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
Returns: List — list of signed byte values (-128 to 127).
Example:
hexToBytes("D8FF"); // [-40, -1]hexToBytesArray
Section titled “hexToBytesArray”Converts a hexadecimal string to a byte array.
hexToBytesArray(hex)| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
Returns: byte[] — byte array.
Example:
hexToBytesArray("D8FF"); // [-40, -1]bytesToHex
Section titled “bytesToHex”Converts a list of bytes to a hexadecimal string.
bytesToHex(bytesList)| Parameter | Type | Description |
|---|---|---|
bytesList |
List or byte[] |
List or array of byte values |
Returns: String — hexadecimal string.
Example:
bytesToHex([0xD8, 0xFF]); // "D8FF"Byte Parsing
Section titled “Byte Parsing”The functions below, from parseBytesToInt to parseBytesLongToDouble, take the same parameters. Pass only data to parse all bytes, or set the range and byte order:
| Parameter | Type | Description |
|---|---|---|
data |
List or byte[] |
Bytes to parse |
offset |
int |
Optional. Index of the first byte to parse (default: 0) |
length |
int |
Optional. Number of bytes to parse (default: all bytes from offset, up to the maximum of the function) |
bigEndian |
boolean |
Optional. Big-endian byte order (default: true). To set it, also pass offset and length. |
parseBytesToInt(data, true) does not set the byte order: true is read as offset 1.
Bytes are not sign-extended: fewer than 4 bytes (8 for parseBytesToLong) always give a non-negative value. For example, parseBytesToInt([0xFF, 0xFF]) returns 65535, and parseBytesToInt([0xFF, 0xFF, 0xFF, 0xFF]) returns -1.
parseBytesToInt
Section titled “parseBytesToInt”Parses up to 4 bytes to an integer.
parseBytesToInt(data[, offset[, length[, bigEndian]]])Returns: int — parsed integer.
Examples:
parseBytesToInt([0xBB, 0xAA]); // 48042parseBytesToInt([0xAA, 0xBB], 0, 2, false); // 48042parseBytesToInt([0x01, 0xBB, 0xAA], 1, 2); // 48042parseBytesToLong
Section titled “parseBytesToLong”Parses up to 8 bytes to a long.
parseBytesToLong(data[, offset[, length[, bigEndian]]])Returns: long — parsed long.
Examples:
parseBytesToLong([0x00, 0x00, 0xBB, 0xAA]); // 48042parseBytesToLong([0xAA, 0xBB, 0x00, 0x00], 0, 4, false); // 48042parseBytesToFloat
Section titled “parseBytesToFloat”Parses up to 4 bytes to a float (IEEE 754).
parseBytesToFloat(data[, offset[, length[, bigEndian]]])Returns: float — parsed float.
Examples:
parseBytesToFloat([0x41, 0xEA, 0x62, 0xCC]); // 29.29824parseBytesToFloat([0xCC, 0x62, 0xEA, 0x41], 0, 4, false); // 29.29824parseBytesToDouble
Section titled “parseBytesToDouble”Parses up to 8 bytes to a double (IEEE 754).
parseBytesToDouble(data[, offset[, length[, bigEndian]]])Returns: double — parsed double.
Examples:
parseBytesToDouble([0x40, 0x9B, 0x04, 0xB1, 0x0C, 0xB2, 0x95, 0xEA]); // 1729.1729parseBytesToDouble([0xEA, 0x95, 0xB2, 0x0C, 0xB1, 0x04, 0x9B, 0x40], 0, 8, false); // 1729.1729parseBytesIntToFloat
Section titled “parseBytesIntToFloat”Interprets up to 4 bytes as an unsigned integer and returns it as a float.
Unlike parseBytesToFloat, this does not use IEEE 754 bit interpretation.
parseBytesIntToFloat(data[, offset[, length[, bigEndian]]])Returns: float — the integer value as a float.
Examples:
parseBytesIntToFloat([0x0A, 0x0A]); // 2570.0parseBytesIntToFloat([0x0A, 0x0A], 0, 1, false); // 10.0parseBytesLongToDouble
Section titled “parseBytesLongToDouble”Interprets up to 8 bytes as an unsigned long and returns it as a double.
Unlike parseBytesToDouble, this does not use IEEE 754 bit interpretation.
parseBytesLongToDouble(data[, offset[, length[, bigEndian]]])Returns: double — the long value as a double.
Example:
parseBytesLongToDouble([0x00, 0x0A, 0x0A]); // 2570.0bytesToExecutionArrayList
Section titled “bytesToExecutionArrayList”Converts a byte array, for example from hexToBytesArray or base64ToBytes, to a list (ExecutionArrayList, TBEL’s internal list type).
bytesToExecutionArrayList(data)| Parameter | Type | Description |
|---|---|---|
data |
byte[] |
Byte array |
Returns: List — execution array list.
Binary Arrays
Section titled “Binary Arrays”parseByteToBinaryArray
Section titled “parseByteToBinaryArray”Converts a single byte to a binary array.
parseByteToBinaryArray(b[, binLength[, bigEndian]])| Parameter | Type | Description |
|---|---|---|
b |
byte |
A single byte value |
binLength |
int |
Optional. Number of bits to return, counted from the lowest bit (default: 8) |
bigEndian |
boolean |
Optional. true to start with the highest bit, false to start with the lowest bit (default: true) |
Returns: byte[] — array of binary digits (0 or 1).
Examples:
parseByteToBinaryArray(0xFF); // [1, 1, 1, 1, 1, 1, 1, 1]parseByteToBinaryArray(0x00); // [0, 0, 0, 0, 0, 0, 0, 0]parseByteToBinaryArray(0x0A, 4); // [1, 0, 1, 0]parseByteToBinaryArray(0x0A, 8, false); // [0, 1, 0, 1, 0, 0, 0, 0]parseBytesToBinaryArray
Section titled “parseBytesToBinaryArray”Converts a list of bytes to a binary array.
parseBytesToBinaryArray(bytesList[, binLength])| Parameter | Type | Description |
|---|---|---|
bytesList |
List or byte[] |
List or array of byte values |
binLength |
int |
Optional. Number of bits to return, counted from the lowest bit (default: 8 bits per byte) |
Returns: byte[] — binary array.
Examples:
parseBytesToBinaryArray([0xFF, 0x00]);// [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]parseBytesToBinaryArray([0xFF, 0x00], 4); // [0, 0, 0, 0]parseLongToBinaryArray
Section titled “parseLongToBinaryArray”Converts a long to a binary array.
parseLongToBinaryArray(value[, binLength])| Parameter | Type | Description |
|---|---|---|
value |
long |
The long value |
binLength |
int |
Optional. Number of bits to return, counted from the lowest bit (default: 64) |
Returns: byte[] — array of binary digits.
Examples:
parseLongToBinaryArray(255L);// [0,0,...,0, 1,1,1,1,1,1,1,1] (56 zeros followed by 8 ones)parseLongToBinaryArray(255, 10); // [0, 0, 1, 1, 1, 1, 1, 1, 1, 1]parseBinaryArrayToInt
Section titled “parseBinaryArrayToInt”Converts a binary array to an integer.
parseBinaryArrayToInt(binaryArray[, offset[, length]])| Parameter | Type | Description |
|---|---|---|
binaryArray |
List or byte[] |
Array of binary digits (0s and 1s) |
offset |
int |
Optional. Index of the first bit to parse (default: 0) |
length |
int |
Optional. Number of bits to parse (default: all bits from offset) |
Returns: int — parsed integer.
An array of exactly 8 bits that starts with 1 is read as a signed byte whenever offset is 0, even with length set: parseBinaryArrayToInt([1, 1, 1, 1, 0, 0, 0, 0], 0, 4) returns -1. To read all 8 bits as unsigned, put a 0 in front: parseBinaryArrayToInt([0, 1, 1, 1, 1, 1, 1, 1, 1]) returns 255.
Examples:
parseBinaryArrayToInt([0, 0, 1, 1, 1, 0, 1, 0]); // 58parseBinaryArrayToInt([1, 1, 1, 1, 1, 1, 1, 1]); // -1parseBinaryArrayToInt([1, 0, 0, 0, 0, 0, 0, 0]); // -128parseBinaryArrayToInt([1, 1, 1, 1, 1, 1, 1, 1], 1, 7); // 127Number Parsing
Section titled “Number Parsing”parseInt
Section titled “parseInt”Parses a string to an integer.
parseInt(str[, radix])| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to parse |
radix |
int |
Optional. Base, for example 2, 8, 10, or 16. By default, it is detected from the string: a string with hex letters is read as hex. A hex string must have an even number of digits: parseInt("FFF") fails, parseInt("0FFF") returns 4095. |
Returns: int — parsed integer.
Examples:
parseInt("123"); // 123parseInt("FF"); // 255parseInt("1010", 2); // 10parseLong
Section titled “parseLong”Parses a string to a long.
parseLong(str[, radix])| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to parse |
radix |
int |
Optional. Base, as in parseInt |
Returns: long — parsed long.
Examples:
parseLong("9223372036854775807"); // 9223372036854775807parseLong("FF", 16); // 255parseFloat
Section titled “parseFloat”Parses a string to a float.
parseFloat(str[, radix])| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to parse |
radix |
int |
Optional. 16 reads the string as IEEE 754 bits. Any other base only checks the digits: the string is still read as a decimal number. |
Returns: float — parsed float.
Examples:
parseFloat("3.14"); // 3.14parseFloat("41200000", 16); // 10.0parseDouble
Section titled “parseDouble”Parses a string to a double.
parseDouble(str)| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to parse |
Returns: double — parsed double.
Example:
parseDouble("3.141592653589793"); // 3.141592653589793Bitwise Operations
Section titled “Bitwise Operations”TBEL supports standard bitwise operators inherited from Java:
| Operator | Description | Example |
|---|---|---|
& |
AND | 0xFF & 0x0F → 0x0F |
| |
OR | 0xF0 | 0x0F → 0xFF |
^ |
XOR | 0xFF ^ 0x0F → 0xF0 |
~ |
NOT (complement) | ~0x00 → -1 |
<< |
Left shift | 1 << 8 → 256 |
>> |
Signed right shift | -256 >> 4 → -16 |
>>> |
Unsigned right shift | -1 >>> 28 → 15 |
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; // 256Base64 Conversion
Section titled “Base64 Conversion”base64ToHex
Section titled “base64ToHex”Converts a Base64-encoded string to a hexadecimal string.
base64ToHex(base64Str)| Parameter | Type | Description |
|---|---|---|
base64Str |
String |
Base64-encoded string |
Returns: String — hexadecimal string.
Example:
base64ToHex("SGVsbG8gV29ybGQ="); // "48656C6C6F20576F726C64"hexToBase64
Section titled “hexToBase64”Converts a hexadecimal string to a Base64 string.
hexToBase64(hex)| Parameter | Type | Description |
|---|---|---|
hex |
String |
Hexadecimal string |
Returns: String — Base64-encoded string.
Example:
hexToBase64("48656C6C6F"); // "SGVsbG8="bytesToBase64
Section titled “bytesToBase64”Converts a list of bytes to a Base64 string.
bytesToBase64(bytesList)| Parameter | Type | Description |
|---|---|---|
bytesList |
List |
List of byte values |
Returns: String — Base64-encoded string.
Example:
bytesToBase64([72, 101, 108, 108, 111]); // "SGVsbG8="base64ToBytes
Section titled “base64ToBytes”Converts a Base64 string to a byte array.
base64ToBytes(base64Str)| Parameter | Type | Description |
|---|---|---|
base64Str |
String |
Base64-encoded string |
Returns: byte[] — byte array.
Example:
base64ToBytes("SGVsbG8gV29ybGQ=");// [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]base64ToBytesList
Section titled “base64ToBytesList”Same as base64ToBytes, but returns a List of byte values.
base64ToBytesList(base64Str)JSON Flattening
Section titled “JSON Flattening”toFlatMap
Section titled “toFlatMap”Flattens a nested JSON object into a single-level map where nested keys are joined with dots. The function takes an object, not a string: to flatten a JSON string, parse it first with decodeToJson.
toFlatMap(json)toFlatMap(json, pathInKey)toFlatMap(json, excludeList)toFlatMap(json, excludeList, pathInKey)Returns: Map — flattened map.
Overload 1 — basic flattening:
var json = {"temperature": 42, "nested": {"humidity": 73}};toFlatMap(json);// {"temperature": 42, "nested.humidity": 73}Overload 2 — with pathInKey flag:
| Parameter | Type | Description |
|---|---|---|
json |
Map |
JSON object to flatten |
pathInKey |
boolean |
true 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:
| Parameter | Type | Description |
|---|---|---|
json |
Map |
JSON object to flatten |
excludeList |
List |
Key names to leave out, matched at any depth (not dotted paths). An excluded object is left out with everything inside it. |
var json = {"temperature": 42, "nested": {"humidity": 73, "pressure": 1013}};toFlatMap(json, ["pressure"]);// {"temperature": 42, "nested.humidity": 73}Overload 4 — with exclusion list and pathInKey:
| Parameter | Type | Description |
|---|---|---|
json |
Map |
JSON object to flatten |
excludeList |
List |
Key names to leave out, matched at any depth (not dotted paths). An excluded object is left out with everything inside it. |
pathInKey |
boolean |
true to include full path in keys |
var json = {"temperature": 42, "nested": {"humidity": 73, "pressure": 1013}};toFlatMap(json, ["pressure"], false);// {"temperature": 42, "humidity": 73}Date and Time (tbDate)
Section titled “Date and Time (tbDate)”The Date class creates dates, reads and changes their parts, formats them, and does date arithmetic.
Creating a Date from a String
Section titled “Creating a Date from a String”new Date(dateString[, pattern[, locale[, timeZone]]])| Parameter | Type | Description |
|---|---|---|
dateString |
String |
The date string to parse. Without pattern, use ISO 8601 with time, for example 2024-01-15T14:30:00Z, or RFC 1123, for example Mon, 15 Jan 2024 14:30:00 GMT. |
pattern |
String |
Optional. Date format pattern, for example dd/MM/yyyy HH:mm |
locale |
String |
Optional. Locale as a language tag, for example en-US or fr-FR. Needed when the date has month or day names. Without it, the server’s locale is used. |
timeZone |
String |
Optional. Time zone, for example UTC or America/New_York. To set it, also pass locale. It overrides any offset in the string. Without it, a date that has no offset is read in the server’s time zone. |
Examples:
var date1 = new Date("2024-01-15T14:30:00Z"); // 2024-01-15T14:30:00Zvar date2 = new Date("Mon, 15 Jan 2024 14:30:00 GMT"); // 2024-01-15T14:30:00Zvar date3 = new Date("2024-01-15 14:30:00.000Z", "yyyy-MM-dd HH:mm:ss.SSSXXX"); // 2024-01-15T14:30:00Zvar date4 = new Date("02:15:30 PM, Sun 10/09/2022", "hh:mm:ss a, EEE M/d/uuuu", "en-US", "America/New_York"); // 2022-10-09T18:15:30Zvar date5 = new Date("15/01/2024 14:30", "dd/MM/yyyy HH:mm", "en-US", "UTC"); // 2024-01-15T14:30:00ZThe string must contain the time: a date alone, such as 2024-01-15 or 15/01/2024 with the pattern dd/MM/yyyy, fails with an error. To parse a date without time, add the time to the string and to the pattern, for example new Date("15/01/2024 00:00", "dd/MM/yyyy HH:mm", "en-US", "UTC").
Creating a Date from Components
Section titled “Creating a Date from Components”Months are numbered from 1 (January). Without a time zone, the server’s time zone is used. Two-digit years are converted: 0–69 become 2000–2069, and 70–99 become 1970–1999.
var date1 = new Date(2024, 1, 15);var date2 = new Date(2024, 1, 15, 14, 30);var date3 = new Date(2024, 1, 15, 14, 30, 45);var date4 = new Date(2024, 1, 15, 14, 30, 45, 500);var date5 = new Date(2024, 1, 15, "UTC");var date6 = new Date(2024, 1, 15, 14, 30, 45, "UTC");Each form with time components also accepts a time zone as the last argument.
Creating a Date from a Timestamp
Section titled “Creating a Date from a Timestamp”var now = new Date(); // current timevar date = new Date(1705329000000); // timestamp in millisecondsdate.toISOString(); // "2024-01-15T14:30:00Z"parseDateToTimestampOrNow
Section titled “parseDateToTimestampOrNow”Parses an ISO 8601 date and time with Z or an offset to a timestamp in milliseconds. Returns the current time if the string cannot be parsed, including a string without a time zone, such as 2024-01-15T14:30:00.
parseDateToTimestampOrNow(dateString)| Parameter | Type | Description |
|---|---|---|
dateString |
String |
Date and time in ISO 8601 format with Z or an offset, for example 2024-01-15T14:30:00Z or 2024-01-15T16:30:00+02:00 |
Returns: long — timestamp in milliseconds.
Examples:
parseDateToTimestampOrNow("2024-01-15T14:30:00Z"); // 1705329000000parseDateToTimestampOrNow("not a date"); // current timeDate Arithmetic
Section titled “Date Arithmetic”Each method changes the date in place and returns nothing. The calculation is done in UTC.
var date = new Date(2024, 1, 15, "UTC");date.addYears(1); // 2025-01-15T00:00:00Zdate.addMonths(2); // 2025-03-15T00:00:00Zdate.addWeeks(1); // 2025-03-22T00:00:00Zdate.addDays(5); // 2025-03-27T00:00:00Zdate.addHours(3); // 2025-03-27T03:00:00Zdate.addMinutes(30); // 2025-03-27T03:30:00Zdate.addSeconds(45); // 2025-03-27T03:30:45Zdate.addNanos(500000000); // 2025-03-27T03:30:45.500Zdate.toISOString(); // "2025-03-27T03:30:45.500Z"There is no addMilliseconds method: to add milliseconds, multiply them by 1,000,000 and use addNanos. Negative values subtract time.
Reading Date Parts
Section titled “Reading Date Parts”The get… getters return the parts of the date in the server’s time zone, and the getUTC… getters return them in UTC.
| Method | Returns |
|---|---|
getFullYear(), getUTCFullYear() |
Year |
getMonth(), getUTCMonth() |
Month, 1 (January)–12 |
getDate(), getUTCDate() |
Day of the month |
getDay(), getUTCDay() |
Day of the week, 1 (Monday)–7 (Sunday) |
getHours(), getUTCHours() |
Hours |
getMinutes(), getUTCMinutes() |
Minutes |
getSeconds(), getUTCSeconds() |
Seconds |
getMilliseconds(), getUTCMilliseconds() |
Milliseconds |
getTime(), valueOf() |
Timestamp in milliseconds |
parseSecond() |
Timestamp in seconds |
Examples:
var date = new Date(2024, 1, 15, 14, 30, 45, 500, "UTC");date.getUTCMonth(); // 1date.getUTCDay(); // 1 (Monday)date.getUTCHours(); // 14date.getHours(); // 16 on a server in the UTC+2 time zonedate.getUTCMilliseconds(); // 500date.getTime(); // 1705329045500date.parseSecond(); // 1705329045Setting Date Parts
Section titled “Setting Date Parts”Each setter changes the date in place and returns nothing. The set… setters use the server’s time zone, and the setUTC… setters use UTC. Months are numbered from 1.
setFullYear(year[, month[, day]])setMonth(month[, day])setDate(day)setHours(hours[, minutes[, seconds[, milliseconds]]])setMinutes(minutes[, seconds[, milliseconds]])setSeconds(seconds[, milliseconds])setMilliseconds(milliseconds)setTime(timestamp)Each setter except setTime has a setUTC… version with the same parameters, for example setUTCMonth(month[, day]).
If you change the year or the month without passing a day and the current day does not exist in the new month, the date moves into the next month. A day you pass yourself must exist in the month, and every value must be in range: setUTCDate(31) in February fails with Invalid date 'FEBRUARY 31', and month 13 fails with Invalid value for MonthOfYear.
Examples:
var date = new Date(2024, 1, 15, "UTC");date.setUTCFullYear(2025);date.setUTCMonth(3);date.setUTCDate(20);date.setUTCHours(8, 15);date.toISOString(); // "2025-03-20T08:15:00Z"
var endOfMonth = new Date(2024, 1, 31, "UTC");endOfMonth.setUTCMonth(2);endOfMonth.toISOString(); // "2024-03-02T00:00:00Z": February 31 becomes March 2
date.setTime(0);date.toISOString(); // "1970-01-01T00:00:00Z"Formatting a Date
Section titled “Formatting a Date”| Method | Returns |
|---|---|
toISOString(), toJSON() |
ISO 8601 string in UTC, for example 2024-01-15T14:30:45.500Z |
toUTCString([locale]) |
Date and time in UTC, for example 2024 Jan 15, Mon 14:30:45 |
toLocaleString([locale[, options]]) |
Date and time |
toLocaleDateString([locale[, options]]) |
Date |
toLocaleTimeString([locale[, options]]) |
Time |
toString([locale[, options]]), toDateString([locale[, options]]), toTimeString([locale[, options]]) |
Date and time, date, or time in the full format. dateStyle and timeStyle are ignored. |
locale is a language tag, for example en-US. Without it, toString, toTimeString, and toLocaleTimeString use the server’s language, and the other methods use a neutral format: toLocaleString() returns, for example, 2024-01-15 16:30:45 on a server in the UTC+2 time zone. options is a time zone, for example "UTC", or a JSON string with any of timeZone, dateStyle, timeStyle, and pattern. dateStyle and timeStyle take short, medium, long, or full, and pattern overrides both. Without a time zone, the server’s time zone is used.
Without dateStyle and timeStyle, the date is short and the time is medium. The exception is toLocaleDateString() or toLocaleDateString(locale) without options, which uses the full date.
Invalid options do not cause an error. An unknown time zone gives the output in GMT. If the JSON has an unknown key, such as timezone instead of timeZone, or a syntax error, all of it is ignored: the output is in GMT with the default styles, even when pattern is set. An unknown style is replaced with the default one.
Examples:
var date = new Date(2024, 1, 15, 14, 30, 45, 500, "UTC");date.toISOString(); // "2024-01-15T14:30:45.500Z"date.toUTCString(); // "2024 Jan 15, Mon 14:30:45"date.toLocaleDateString("en-US", "UTC"); // "1/15/24"date.toLocaleDateString("en-US"); // "Monday, January 15, 2024" on a server in the UTC+2 time zonedate.toLocaleTimeString("en-US", "UTC"); // "2:30:45 PM"date.toString("en-US", "UTC"); // "Monday, January 15, 2024, 2:30:45 PM Coordinated Universal Time"date.toLocaleString("en-US", '{"timeZone":"UTC","dateStyle":"full","timeStyle":"short"}'); // "Monday, January 15, 2024, 2:30 PM"date.toLocaleString("en-US", '{"timeZone":"UTC","pattern":"yyyy-MM-dd HH:mm"}'); // "2024-01-15 14:30"The space before PM is a narrow no-break space (U+202F). The output of locale-dependent methods depends on the Java version: to get the same output on every version, use toISOString or a numeric pattern. See the caution at the start of Date and Time.
Static Date Methods
Section titled “Static Date Methods”| Method | Returns |
|---|---|
Date.now() |
Current timestamp in milliseconds |
Date.UTC(year, month, day[, hours[, minutes[, seconds[, milliseconds]]]]) |
Timestamp of a date and time in UTC. Months are numbered from 1; pass a four-digit year. |
Date.parse(dateString[, pattern]) |
Timestamp of the parsed date, or -1 if the string cannot be parsed. See below. |
Without pattern, Date.parse needs a date and time such as 2024-01-15T14:30:00Z; a space can replace T. A string without an offset is read in the server’s time zone. A date alone, such as 2024-01-15, or an RFC 1123 string returns -1.
With pattern, a date without an offset is read in the server’s time zone. The pattern uses the letters of Java SimpleDateFormat: write years as yyyy, not uuuu. Values out of range are not rejected but roll over, for example January 32 becomes February 1.
Examples:
Date.now(); // current timeDate.UTC(2024, 1, 15); // 1705276800000Date.UTC(2024, 1, 15, 14, 30); // 1705329000000Date.parse("2024-01-15T14:30:00Z"); // 1705329000000Date.parse("2024-01-15 14:30:00Z"); // 1705329000000Date.parse("2024-01-15T14:30:00"); // 1705321800000 on a server in the UTC+2 time zoneDate.parse("15/01/2024 14:30", "dd/MM/yyyy HH:mm"); // 1705321800000 on a server in the UTC+2 time zoneDate.parse("32/01/2024 14:30", "dd/MM/yyyy HH:mm"); // 1706790600000 (February 1) on a server in the UTC+2 time zoneDate.parse("2024-01-15"); // -1Date.parse("not a date"); // -1Geofencing
Section titled “Geofencing”isInsidePolygon
Section titled “isInsidePolygon”Checks whether a geographic point lies inside a polygon.
isInsidePolygon(latitude, longitude, perimeter)| Parameter | Type | Description |
|---|---|---|
latitude |
double |
Point latitude |
longitude |
double |
Point longitude |
perimeter |
String or List |
Polygon vertices as [[latitude, longitude], …]: a list, or a JSON string such as the value of a perimeter attribute. Two points define a rectangle by its opposite corners. A list of polygons defines several areas; a polygon that lies inside another one is a hole. |
Returns: boolean — true if the point is inside the polygon.
Example:
var polygon = [ [50.45, 30.50], [50.45, 30.55], [50.40, 30.55], [50.40, 30.50]];isInsidePolygon(50.43, 30.52, polygon); // trueisInsideCircle
Section titled “isInsideCircle”Checks whether a geographic point lies inside a circle.
isInsideCircle(latitude, longitude, perimeter)| Parameter | Type | Description |
|---|---|---|
latitude |
double |
Point latitude |
longitude |
double |
Point longitude |
perimeter |
String |
Circle as a JSON string with latitude and longitude of the center, radius, and an optional radiusUnit: METER (default), KILOMETER, FOOT, MILE, or NAUTICAL_MILE. For example, the value of a perimeter attribute. |
Returns: boolean — true if the point is inside the circle.
Example:
var circle = '{"latitude": 50.45, "longitude": 30.52, "radius": 1.5, "radiusUnit": "KILOMETER"}';isInsideCircle(50.455, 30.525, circle); // trueWas this helpful?