@iota/converter v1.0.0-beta.a1883550
@iota/converter
Methods for converting ascii, values & trytes to trits and back.
Installation
Install using npm:
npm install @iota/converteror using yarn:
yarn add @iota/converterAPI Reference
converter.asciiToTrytes(input)
| Param | Type | Description |
|---|---|---|
| input | string | ascii input |
Converts an ascii encoded string to trytes.
How conversion works:
An ascii value of 1 Byte can be represented in 2 Trytes:
We get the decimal unicode value of an individual ASCII character.
From the decimal value, we then derive the two tryte values by calculating the tryte equivalent (e.g.:
100is expressed as19 + 3 * 27), given that tryte alphabet contains27trytes values: a. The first tryte value is the decimal value modulo27(which is the length of the alphabet). b. The second value is the remainder ofdecimal value - first valuedevided by27.The two values returned from Step 2. are then input as indices into the available trytes alphabet (
9ABCDEFGHIJKLMNOPQRSTUVWXYZ), to get the correct tryte value.
Example:
Lets say we want to convert ascii character Z.
Zhas a decimal unicode value of90.90can be represented as9 + 3 * 27. To make it simpler: a. First value is90 % 27 = 9. b. Second value is(90 - 9) / 27 = 3.Our two values are
9and3. To get the tryte value now we simply insert it as indices into the tryte alphabet: a. The first tryte value is'9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[9] = Ib. The second tryte value is'9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[3] = C
Therefore ascii character Z is represented as IC in trytes.
Returns: string - string of trytes
converter.trytesToAscii(trytes)
| Param | Type | Description |
|---|---|---|
| trytes | string | trytes |
Converts trytes of even length to an ascii string
Returns: string - string in ascii
converter.trits(input)
| Param | Type | Description |
|---|---|---|
| input | String | Number | Tryte string or value to be converted. |
Converts trytes or values to trits
Returns: Int8Array - trits
converter.trytes(trits)
| Param | Type |
|---|---|
| trits | Int8Array |
Converts trits to trytes
Returns: String - trytes
converter.value(trits)
| Param | Type |
|---|---|
| trits | Int8Array |
Converts trits into an integer value
converter.fromValue(value)
| Param | Type |
|---|---|
| value | Number |
Converts an integer value to trits
Returns: Int8Array - trits
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago