Bytes vs Strings in Solidity

Bytes vs Strings in Solidity

Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any sequences of characters visible or invisible, and characters may be repeated. Solidity supports string literal using both double quote (" ") and single quote (' ').

  • 'hello' - is a string;
  • "hello" - is a string;
  • "37" - is a string;
  • 37 - is not a string, it is an integer.

How we actually write a string in #solidity ? We just type in string (which is a datatype in solidity), then give it a name (for example 'greetings") and we can set what we want inside the string - we bring in our quotations and type in what we want the string to be (for example 'Hello!').

No alt text provided for this image

  • string - is the datatype;
  • greetings - is the name of the string;
  • 'Hello!' - is the string literal.

Now that we have this 'hello' string, what can we actually do with in in our contract? Why don't we create a function that returns our string? Let's create a function named sayHello that will return a string. It is important to know that you can not just return a string in solidity because it has to go somewhere, a string has to be stored. In order to store it, we want to let solidity know to store our string to memory.

No alt text provided for this image

Memory is much like RAM. Memory in Solidity is a temporary place to store data whereas storage holds data between functions. The Solidity Smart Contract can use any amount of memory during execution, but once the execution stops, the memory is completely wiped off for the next execution.

So in our function we want to return our greetings, our string value.

No alt text provided for this image

It is working :) The key takeaway here is that we've created a string called greetings and then we initialized it to 'Hello!'. Next, we have a function that returns this 'Hello!'.

Now, what if we want to change our greetings, make it something else? Well, we could create another function that essentially is going to take an argument (an input) where we can actually change what the greetings is and then return the new greetings.

Let's name the new function called changeGreeting and give it an argument of _change (string type and and add it to memory), make it public without returning anything. Set the greetings from our first function to the input to our _change from this function.

No alt text provided for this image

Now, let's deploy the contract. We can see that we have our two functions: sayHello and changeGreeting . If I click sayHello we will get string: Hello!

No alt text provided for this image

If we want to change the greetings to Romanian, we will type 'Buna ziua!' and click the button changeGreeting first and the button sayHello again:

No alt text provided for this image

It is working :)

Well, sometimes we want to manipulate the strings or we want to get access to their characters, figure out what's going on in these strings. So, how do we do this in solidity? It's a little bit more tricky in solidity to do this than it is in other languages. For example in JavaScript we can use return greetings.length and it will work - we will receive the result 6 (there a six characters in the string 'Hello!'). Length is a method which can get us the length of our string (greetings). In solidity it is not possible to do this.

No alt text provided for this image

Why it is not working? This doesn't work because solidity doesn't want it to work - it would be very expensive for solidity to hash this out. Strings are too expensive computationally to get length in solidity so you can't do it like other languages. In solidity we should convert strings into bytes. This is very important concept in solidity, because working with bytes in general saves computational expenses as opposed to working with strings. Thinking about bytes and strings together is important to consider.

So, how would we convert our 'Hello!' into bytes? There is another datatype called bytes. Bytes is like strings only instead of registering characters in string format, it's going to go to the basic byte format. Bytes are the basic unit of measurement in computer processing. Computers are speaking in bytes, they understanding everything in bytes.

Let's convert our string to bytes by writing in our contract the datatype bytes and then create a name stringToBytes and what this is going to equal is our conversion. We can do this with bytes and then what do we want to be converting - greetings. Also important to note that bytes need to be placed in storage as well, so we will use memory.

No alt text provided for this image

And now, in our function, what will we return? We can use leverage the length of greetings, but instead of grabbing it from greetings, we can grab the converted version in stringToBytes.

No alt text provided for this image

Now, let's deploy the contract!

No alt text provided for this image

This is essentially the key important things to consider when thinking about strings.

See you in the next articles about #blockchain , #remix , #solidity , #crypto !

要查看或添加评论,请登录

Ion Platon的更多文章

  • Storage vs Memory in Solidity

    Storage vs Memory in Solidity

    Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is…

  • What is Mapping in Solidity?

    What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • Struct and Enum in Solidity

    Struct and Enum in Solidity

    A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs…

  • What are Libraries in Solidity?

    What are Libraries in Solidity?

    Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other…

  • What are Interfaces in Solidity?

    What are Interfaces in Solidity?

    Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics…

  • What is Inheritance in Solidity?

    What is Inheritance in Solidity?

    Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in…

  • The Restricted Access Pattern in Solidity

    The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Loops in Solidity

    Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

社区洞察

其他会员也浏览了