Solidity Remix trivia questions

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

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

Hopefully someone can provide 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);
    }     }

      

+3


source to share


3 answers


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

pragma solidity ^0.4.0;

contract MyContract {

}

      



From Hardness Documentation :

Pragma version

Source files can (and should) be annotated with the so-called version of the pragma to refuse to compile with future versions of the compiler, which may lead to incompatible changes. We try to keep such changes to an absolute minimum, and especially making changes in semantics will also require changes in syntax, but this of course is not always possible. Because of this it is always a good idea to read the changelog, at least for releases containing breaking changes, these 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 the compiler prior to version 0.4.0, and it will also not work for the compiler since version 0.5.0 (this second condition is added using ^). The idea behind this is that it won't change until version 0.5.0, so we can always be sure that our code will compile as we intended. We don't fix the exact compiler versions, so fix releases are still possible.

+2


source


As mentioned above, you need to include the compiler version on the first line of the integrity code:



pragma solidity ^ 0.4.0;

+1


source


This code is indeed compiled and this is a warning: warning.

suggested in durability docs to indicate the compiler version to reject compilation by compiler versions that might make changes.

Try adding pragma solidity ^0.4.11;

(or some other version) to the beginning of the file and you will see the warning goes away.

Your complete file will 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);
    }     
}

      

0


source







All Articles