Skip to content

fromBase

Description

Converts a string representation of a number in a given base into its numeric value.

For example, "101" in base 2 becomes 5 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 of the string representation (e.g. hexadecimal is base 16)

Example

Use case: Parse a hexadecimal string

Convert a hexadecimal string to a numeric value so it can be used in calculations.

{
    "hex_value": "FF00FF"
}
create num_value from fromBase(hex_value, 16)
create num_value from hex_value.fromBase(16)

Output

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