Namespace: Conversion:fromBaseX

Conversion:fromBaseX

Methods

fromBaseX(value, baseOrAlphabetopt, useCharacterMapopt, nullable, useChunksopt, nullable, valueIsNumberopt, nullable) → {String}

This function converts a based representation back to its original number or string value. This is the mirror function to toBaseX() and expects a value encoded with that function. See that function for implementation details, modes and restrictions.

The result of this function is always either a decimal number or a string, just as the input value. All numbers apart from decimal ones are returned as strings without prefix. So, decimal 5 will be the number 5, but the binary version will be the string "101".

You may define the base as an integer between 2 and 64 or as a custom alphabet in the same range. Integer based alphabets are generated using defined base alphabets, which are sliced if necessary. Custom alphabets are automatically sorted to match base64 are far as possible, pushing additional characters to the end, which are then sorted ascending by character value.

"{" and "}" are the only forbidden characters in a custom alphabet, since we need these to mark number values in fromBaseX().

Numerical conversion keeps negative numbers negative and marks the result with a preceding "-".

Parameters:
Name Type Attributes Default Description
value Number | String

value to be decoded

baseOrAlphabet Number | String | Array.<String> <optional>
64

either the numerical base to convert to (64, 36, ...) or the alphabet of characters to use in encoding; numerical bases must be between 2 and 64 (if the result is chunked, we need a base 3)

useCharacterMap Boolean <optional>
<nullable>
true

set to true, to use a character map, based on btoa(), instead of numerical conversion

useChunks Boolean <optional>
<nullable>
false

set to true, to add chunking to the numerical approach, converting the value in groups separated by a delimiter, which is the first letter of the base's alphabet

valueIsNumber Boolean <optional>
<nullable>
false

if true, the given value is treated as a number for numerical conversion; this is necessary, since numbers such as binaries are defined as strings and are therefore not auto-detectable

Source:
See:
Throws:
  • error if baseOrAlphabet is not usable

  • error character mapped decoding fails, due to missing token/unmatched alphabet

Returns:
the decoded value
Type
String
Example
fromBaseX('Zm9vYmFy')
=> 'foobar'
fromBaseX('16W33YPUS', 36)
=> 'äす'
fromBaseX('{-3C3}', 13)
=> -666
fromBaseX('q', 64, false, false, true)
=> 42
fromBaseX('U70R0DCN0F0DS04T0BQ040R0GCN0N0JSNA03TZ0J01S0K0N0KQOA0HRN0R0C', 36, true)
=> 'too-long-for-number-conversion'
fromBaseX('D3EF5D81F026D9DFDA970BBF17222402A47D5AD650CF6C2FE2102A494BCBDD0A2864C', 16, false, true)
=> 'too-long-for-number-conversion'