iota-ternary
Version:
Fast utility functions for conversion between trytes, trits and bytes
50 lines (44 loc) • 757 B
text/typescript
import { trit } from "../types"
const NUMBERS_TO_TRYTES = {
[]: "9",
[]: "A",
[]: "B",
[]: "C",
[]: "D",
[]: "E",
[]: "F",
[]: "G",
[]: "H",
[]: "I",
[]: "J",
[]: "K",
[]: "L",
[]: "M",
[-13]: "N",
[-12]: "O",
[-11]: "P",
[-10]: "Q",
[-9]: "R",
[-8]: "S",
[-7]: "T",
[-6]: "U",
[-5]: "V",
[-4]: "W",
[-3]: "X",
[-2]: "Y",
[-1]: "Z",
}
export function tritsToTrytes(trits: trit[]): string {
const size = Math.ceil(trits.length / 3)
const trytes = new Array(size)
for (var i = 0; i < size; i += 1) {
trytes[i] = NUMBERS_TO_TRYTES[
(trits[i*3 + 0] || 0)
+
(trits[i*3 + 1] || 0)*3
+
(trits[i*3 + 2] || 0)*9
]
}
return trytes.join('')
}