5

In the limited documentation on Serpent, it mentions the ability to use a str type as both input and output to a contract function. Short strings are converted to numbers and longer text uses the text() function.

Say I have this simple Serpent contract function using short strings:

def my_name_is(n):
  return(n)

If you call this, passing in the value 'bob', it'll return a numeric value for the str 'bob'. How would you convert the numeric value back to the str 'bob'? I've looked over many examples and can't find any that will actually work for this example.

  • If you've answered your own question, you can also post its answer (remove the edit from the question). In the answer, it seems it would be helpful to clarify what the numeric value of 'bob' is and how/what is converted back to 'bob'. – eth Mar 12 '16 at 19:08
4

Strings in Serpent are represented 2 different ways. Short strings created with quotes e.g. "bob" are represented as integers. While longer strings, those created by using Serpent's text() function are treated internally as an array.

Serpent provides 2 functions you can use to store and load strings - save() and load(). Here's an example:

data info[](title[100], titleLength)

def saveTitle(id, title:str):
  save(self.info[id].title[0],title,chars=len(title))
  self.info[id].titleLength = len(title)
  return(1)

def getTitle(id):
  length=self.info[id].titleLength
  return( load(self.info[id].title[0],chars=length):str ) 

Unfortunately these functions are not well documented. If you're interested in other Serpent functionality, I'd suggest looking through rewriter.cpp in Serpent's source code.

  • If you are satisfied with your own answer :) you can mark it as accepted so others can see that this solved the problem for you. – Joris Bontje Mar 19 '16 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.