1

I'm wondering why string is used to define token parameters such as name, and symbol? Why can't I use any form of bytes such as bytes8 etc.

I understand that it's a public parameter and non-devs will be able to check up the name etc in string encoded readable format but I want to use bytes where ever possible to decrease gas-costs.

How would I go about describing following line in bytes format? string constant public name = "Nowsy Token"

4
  • How would I go about describing following line in bytes format? string constant public name = "Nowsy Token"
    – NowsyMe
    Oct 17, 2017 at 15:05
  • bytes11 constant public name = "Nowsy Token"; Oct 17, 2017 at 15:09
  • You can edit the question if the duplicate marked question doesn't answer your problem :) Oct 17, 2017 at 15:09
  • If you add the answer ,then i can upvote it :)
    – NowsyMe
    Oct 17, 2017 at 15:13

1 Answer 1

1

Why can't I use any form of bytes such as bytes8 etc

yes, you can use the fixed-size byte array , if you are sure that the length of it would not be longer than that. Say byte8, it has to be less than 8 characters.

From the docs,

As a rule of thumb, use bytes for arbitrary-length raw byte data and string for arbitrary-length string (UTF-8) data. If you can limit the length to a certain number of bytes, always use one of bytes1 to bytes32 because they are much cheaper.

And for you requirement,

How would I go about describing following line in bytes format? string constant public name = "Nowsy Token"

you can use bytes11 constant public name = "Nowsy Token"; since it's constant and you know the size of it's 11.

3
  • But i would have to convert "Nowsy Token" to bytes yes? otherwise it would implicitely not understand the format
    – NowsyMe
    Oct 17, 2017 at 15:17
  • now it's in a fixed-size byte array type of byte11, you can just use it Oct 17, 2017 at 15:18
  • 1
    @NowsyMe Notice that if you intend to make your token ERC20 compliant you shouldn't change the name data type. Oct 23, 2017 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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