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

Like many functions in DataPrime, fromBase supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

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

Arguments

NameTypeRequiredDescription
stringstringtrueThe string containing the number
radixnumbertrueThe 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 fromBase(hex_value, 16)
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: