Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pass variable length var. between contracts. Metropolis added support for that, but solc still does not support that directly.
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