Skip to content

Instantly share code, notes, and snippets.

@AnthonyAkentiev
Created December 20, 2017 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonyAkentiev/68c90721d062b7b21879af7b74b1e95e to your computer and use it in GitHub Desktop.
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.
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