Created
December 20, 2017 10:06
-
-
Save AnthonyAkentiev/68c90721d062b7b21879af7b74b1e95e to your computer and use it in GitHub Desktop.
Pass variable length var. between contracts. Metropolis added support for that, but solc still does not support that directly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.19; | |
contract One { | |
string public s = "two"; | |
function returnString() public constant returns(string) { | |
return "one"; | |
} | |
function stringToBytes32(string s) constant returns(bytes32){ | |
bytes32 out; | |
assembly { | |
out := mload(add(s, 32)) | |
} | |
return out; | |
} | |
function returnStringAsBytes() public constant returns(bytes32) { | |
return stringToBytes32(s); | |
} | |
} | |
contract Two { | |
One one = new One(); | |
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 getString() public constant returns(string) { | |
bytes32 s = one.returnStringAsBytes(); | |
string memory out = bytes32ToString(s); | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment