I want to calculate string length in bytes. Here is the sample code I used.

function mine(string s  ) public view returns (bytes) {
        return bytes(s).length;

But throws this error-

TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) bytes memory.
         return bytes(s).length;

If you want to return byte-length of the string, you will need to modify your return type like this:

function mine(string s) public view returns (uint256) {
  return bytes(s).length;

This method will only return byte-length of your string.

For UTF-string length, follow Alexey's answer in detail here: https://ethereum.stackexchange.com/a/13886/5018


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.