Module: IOTA::Utils::Ascii
- Included in:
- Utils
- Defined in:
- lib/iota/utils/ascii.rb
Constant Summary collapse
- TRYTE_VALUES =
"9ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Instance Method Summary collapse
Instance Method Details
#fromTrytes(input) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/iota/utils/ascii.rb', line 29 def fromTrytes(input) # If input is invalid trytes or input length is odd return nil if !@validator.isTrytes(input) || input.length % 2 != 0 outputString = "" (0...input.length).step(2) do |i| trytes = input[i] + input[i + 1] firstValue = TRYTE_VALUES.index(trytes[0]) secondValue = TRYTE_VALUES.index(trytes[1]) decimalValue = firstValue + secondValue * 27 outputString += decimalValue.chr end outputString end |
#toTrytes(input) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/iota/utils/ascii.rb', line 6 def toTrytes(input) # If input is not a string, return nil return nil if !@validator.isString(input) trytes = "" (0...input.length).step(1) do |i| char = input[i] asciiValue = char.bytes.sum # If not recognizable ASCII character, return null return nil if asciiValue > 255 firstValue = asciiValue % 27 secondValue = (asciiValue - firstValue) / 27 trytesValue = TRYTE_VALUES[firstValue] + TRYTE_VALUES[secondValue] trytes += trytesValue end trytes end |