Skip to content

Instantly share code, notes, and snippets.

@christoph2806
Created January 21, 2017 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christoph2806/5317a81db6721eb55c1af50e549c9d67 to your computer and use it in GitHub Desktop.
Save christoph2806/5317a81db6721eb55c1af50e549c9d67 to your computer and use it in GitHub Desktop.
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