// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; library StringToBytes32 { error InvalidStringLength(); function toBytes32(string memory str) internal pure returns (bytes32) { // Converting a string to bytes32 for immutable storage bytes memory stringBytes = bytes(str); // We can store up to 31 bytes of data as 1 byte is for encoding length if (stringBytes.length == 0 || stringBytes.length > 31) revert InvalidStringLength(); uint256 stringNumber = uint256(bytes32(stringBytes)); // Storing string length as the last byte of the data stringNumber |= 0xff & stringBytes.length; return bytes32(stringNumber); } } library Bytes32ToString { function toTrimmedString(bytes32 stringData) internal pure returns (string memory converted) { // recovering string length as the last byte of the data uint256 length = 0xff & uint256(stringData); // restoring the string with the correct length assembly { converted := mload(0x40) // new "memory end" including padding (the string isn't larger than 32 bytes) mstore(0x40, add(converted, 0x40)) // store length in memory mstore(converted, length) // write actual data mstore(add(converted, 0x20), stringData) } } }