4

Am new to Solidity here, this is the code I am testing and remix spits out

browser/Untitled.sol:1:1: : Source file does not specify required compiler version!Consider adding "pragma solidity ^0.4.12 contract C { ^ Spanning multiple lines.

Hopefully someone can give some guidance.

contract C {
    function bytes32ToString(bytes32 x) constant returns (string) {
        bytes memory bytesString = new bytes(32);
        uint charCount = 0;
        for (uint j = 0; j < 32; j++) {
            byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
            if (char != 0) {
                bytesString[charCount] = char;
                charCount++;
            }
        }
        bytes memory bytesStringTrimmed = new bytes(charCount);
        for (j = 0; j < charCount; j++) {
            bytesStringTrimmed[j] = bytesString[j];
        }
        return string(bytesStringTrimmed);
    }

    function bytes32ArrayToString(bytes32[] data) returns (string) {
        bytes memory bytesString = new bytes(data.length * 32);
        uint urlLength;
        for (uint i=0; i<data.length; i++) {
            for (uint j=0; j<32; j++) {
                byte char = byte(bytes32(uint(data[i]) * 2 ** (8 * j)));
                if (char != 0) {
                    bytesString[urlLength] = char;
                    urlLength += 1;
                }
            }
        }
        bytes memory bytesStringTrimmed = new bytes(urlLength);
        for (i=0; i<urlLength; i++) {
            bytesStringTrimmed[i] = bytesString[i];
        }
        return string(bytesStringTrimmed);
    }     }
Don
  • 3,876
  • 10
  • 47
  • 76
metalbean
  • 101
  • 2
  • 4

4 Answers4

4

Include a version pragma at the very top of the source file to get rid of the warning.

pragma solidity ^0.4.0;

contract MyContract {

}

From Solidity documentation:

Version Pragma

Source files can (and should) be annotated with a so-called version pragma to reject being compiled with future compiler versions that might introduce incompatible changes. We try to keep such changes to an absolute minimum and especially introduce changes in a way that changes in semantics will also require changes in the syntax, but this is of course not always possible. Because of that, it is always a good idea to read through the changelog at least for releases that contain breaking changes, those releases will always have versions of the form 0.x.0 or x.0.0.

The version pragma is used as follows:

pragma solidity ^0.4.0;

Such a source file will not compile with a compiler earlier than version 0.4.0 and it will also not work on a compiler starting from version 0.5.0 (this second condition is added by using ^). The idea behind this is that there will be no breaking changes until version 0.5.0, so we can always be sure that our code will compile the way we intended it to. We do not fix the exact version of the compiler, so that bugfix releases are still possible.

Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
2

As everyone mentioned above, you need to specify the compiler version in the first line of the solidity code:

pragma solidity ^0.4.0;

Abhishek
  • 176
  • 1
  • 12
1

This code is actually compiled, and the warning is just that: a warning.

It's suggested in the solidity docs to specify a compiler version, to reject compilation by compiler versions that may introduce breaking changes.

Try adding pragma solidity ^0.4.11; (or some other version) to the top of your file, and you'll see the warning disappear.

Your full file would now be:

pragma solidity ^0.4.11;

contract C {
    function bytes32ToString(bytes32 x) constant returns (string) {
        bytes memory bytesString = new bytes(32);
        uint charCount = 0;
        for (uint j = 0; j < 32; j++) {
            byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
            if (char != 0) {
                bytesString[charCount] = char;
                charCount++;
            }
        }
        bytes memory bytesStringTrimmed = new bytes(charCount);
        for (j = 0; j < charCount; j++) {
            bytesStringTrimmed[j] = bytesString[j];
        }
        return string(bytesStringTrimmed);
    }

    function bytes32ArrayToString(bytes32[] data) returns (string) {
        bytes memory bytesString = new bytes(data.length * 32);
        uint urlLength;
        for (uint i=0; i<data.length; i++) {
            for (uint j=0; j<32; j++) {
                byte char = byte(bytes32(uint(data[i]) * 2 ** (8 * j)));
                if (char != 0) {
                    bytesString[urlLength] = char;
                    urlLength += 1;
                }
            }
        }
        bytes memory bytesStringTrimmed = new bytes(urlLength);
        for (i=0; i<urlLength; i++) {
            bytesStringTrimmed[i] = bytesString[i];
        }
        return string(bytesStringTrimmed);
    }     
}
Travis
  • 2,579
  • 18
  • 19
1

See the responses given here and want to be clear with the compiler version:

In this case, you should use pragma solidity 0.4.11; if that is the compiler version that you have been testing and intend to deploy from. If you add ^ you don't lock the version and the risk of bugs will be considerably higher, especially if anyone other than the author will deploy the contract. If you lock the compiler version, you can ensure that the code will not be compiled with another version and the one that you intended.

Note that Solidity has a new code pattern here: pragma solidity >=0.4.24 <0.6.0; but you can still lock the version pragma solidity 0.5.2;.

NemboKid
  • 105
  • 1
  • 9