raineorshine
1/21/2017 - 8:09 AM

ConvertLib

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();
        
    }
}