1

I encountered this problem when I designed a test contract and initialized the mapping therein as provided below.

pragma solidity ^0.4.10;

contract test{

    mapping (uint256 => string) uintTostring;

    uintTostring[uint256(1)]="1";
}

The compiler says there is parse error about this command " uintTostring[1]='1' " Where do I make wrong and how I can solve this problem?

0

You need to create a setter function to set the value of the mapping variable "uintTostring".

Below is a very basic and simple example for the same:

pragma solidity ^0.4.10;
contract test{
    mapping (uint256 => string) uintTostring;
    function setValueOfVariable() {
        uintTostring[1] = "1";
    }
}

You can also pass the parameters to this function to make it more flexible. Also to retrieve the variable a getter function would be required or declare the variable as public.

Hope this helps..!

| improve this answer | |
0

try this code :

pragma solidity ^0.4.10;

contract test{

function bytes32ToString (bytes32 data) returns (string) {
    bytes memory bytesString = new bytes(32);
    for (uint j=0; j<32; j++) {
        byte char = byte(bytes32(uint(data) * 2 ** (8 * j)));
        if (char != 0) {
            bytesString[j] = char;
        }
    }
    return string(bytesString);
}
    mapping (uint256 => string) uintTostring;

   function f(uint i){
       uintTostring[i]=bytes32ToString(bytes32(i));
   } 
}

and read about conversion of uint to string : Conversion of uint to string

| improve this answer | |

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.