Code Monkey home page Code Monkey logo

trytes2csv's Introduction

Trytes2CSV

Translate trytes directly to alphanumeric characters and keep the trytes somewhat readable directly in a tangle explorer.

This will help run projects on small devices doing datalogging by reducing the pow for larger than 440 Bytes of data.

A csv file in a single transaction can now contain 670 double digit measurements or at least 2000 characters.

This is an example of data turnary: XFL99XCLAY9SXCOUNTY9BFEHBAJEG9O9AEGEO

Which should result in the following ASCII data: FL,CLAY COUNTY,265821.57,0,15750

trytes2csv's People

Contributors

logiota avatar

trytes2csv's Issues

not sure what exactly you want to do

but all you do is translate from the alphabet list (tern) to numeric value (data list) and vice versa but that ain't no trytes or tryte conversion as far as i can see, if that is what you want to do. perhaps search on github for python projects which already do this? otherwise going after this:
https://steemit.com/iota/@wiredcrypto/understanding-iota-trits-and-trytes

First, each letter must be assigned its ASCII decimal value.
For H, the ASCII value is 72, for O 79, for D 68 and L 76.

ascii_val = ord('H')

Then the respective ASCII decimal value is modulated by 27, the number of characters of the IOTA alphabet.
The resulting remainder results in the first part of the tryte.
The remainder defines the position of the letter in the IOTA alphabet.

# we can hardcode it like this
iota_alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '9']

# or generate it somewhat like this
import string
iota_alphabet = list(string.ascii_uppercase)
iota_alphabet.append(9)

# we know it's 27 characters long, A-Z and a 9, so we could just hardcode 27
remainder =  ascii_val % 27

# or get it's length
remainder = ascii_val % len(iota_alphabet)

Then the remainder value is subtracted from the ASCII value and divided by the number of characters of the IOTA
alphabet to obtain the second value of the tryte or the position of the second letter in the IOTA alphabet.

tryte_val_two = (ascii_val - remainder) / 27

# or again getting the length
tryte_val_two = (ascii_val - remainder) / len(iota_alphabet)

# this gives us a float val, we need an int
# so we make it one
tryte_val_two = int((ascii_val - remainder) / len(iota_alphabet))

# could also be done when getting the char from the list like this
iota_alphabet[int(tryte_val_two)]

# now we have the two characters for the trye, or at least their index in the alphabet
# let's get this done
tryte = iota_alphabet[remainder] + iota_alphabet[tryte_val_two]

looks great, but doesn't help that much in this cluttered fashion, so let's put this together in a function

def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int(ascii_val % len(iota_alphabet))
	tc2 = int((ascii_val - tc1) / len(iota_alphabet))
	tryte = iota_alphabet[tc1] + iota_alphabet[tc2]

	return tryte

and run this

print(char_to_tryte('H'))
SC

# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2

this is working and gives us the right numbers but the return value looks kinda wrong. we wanted RB and got SC. luckily we know python lists start at 0, not 1. so what we need to do is subtract 1 from our values somewhere. either directly while getting them or while using them as list indices

# as list indices:
def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int(ascii_val % len(iota_alphabet))
	tc2 = int((ascii_val - tc1) / len(iota_alphabet))
	tryte = iota_alphabet[tc1 - 1] + iota_alphabet[tc2 - 1]

	return tryte

# while getting the numbers
def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int((ascii_val % len(iota_alphabet)) - 1)
	tc2 = int(((ascii_val - tc1) / len(iota_alphabet)) - 1)
	tryte = iota_alphabet[tc1] + iota_alphabet[tc2]

	return tryte

print(char_to_tryte('H'))
# now gives us
RB

# when subtracting from list indices
# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2

# when subtracting while getting our numbers
# ascii_val -> 72
# tc1 -> 17
# tc2 -> 1

so while this works, it's def not a good function and i have tried and failed with reversing this from tryte to char have a look at how its done here:
https://github.com/iotaledger/iota.lib.py/blob/master/iota/codecs.py

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.