Skip to content

fromBase - Parses a string representation of a number in a given base

fromBase will parse a string value, representing some number, in a given base. For example, 01 in base 2 is equal to 1 in base 10.

Syntax

fromBase(string: string, radix: number): number

Arguments

Name Type Required Description
string string true The string containing the number
radix number true The base for the string-number value. For example, hexidecimal has base 16.

Example - Parsing a hexidecimal string

Consider the following document:

{
    "hex_value": "FF00FF"
}

Using the fromBase function, we can parse this hexidecimal value into a number type, which allows us to perform further numerical calculations.

create num_value from hex_value.fromBase(16)

This results in the following document:

{
    "hex_value": "FF00FF",
    "num_value": 16711935
}

This can also be written as a standalone function:

create num_value from fromBase(hex_value, 16)