Created
January 21, 2017 08:09
-
-
Save christoph2806/5317a81db6721eb55c1af50e549c9d67 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.7; | |
library convertLib { | |
function b32toString(bytes32 x) internal returns (string) { | |
// gas usage: about 1K gas per char. | |
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); | |
} | |
} | |
contract A { | |
function getBytes32() returns (bytes32) { | |
return 'hello World'; | |
} | |
} | |
contract B { | |
using convertLib for *; | |
A a; | |
function test () { | |
string memory s = a.getBytes32().b32toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment