pragma solidity ^0.4.18; contract MyStrings { event Log(string message); function doSomething() public { bytes32 name = "Zia";//dont use var name = "Mark"; Log(bytes32ToString(name)); bytes32 nationality; nationality = "Pak"; Log(bytes32ToString(nationality)); } function bytes32ToString(bytes32 x) internal pure 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); } }
0.4.18