How to convert a string to bytes32?

  • How can I convert a string to a bytes32? Does anyone has a magic function or library which does it?

    For example: This works since as input I gave 32-bits. But it won't work with "> 32-bit" chars.

    set_txn_hash = a.transact().set("QmVoGzRDscx61k3RHHkLYaMFtxYZi3ps");#32-bit char


    function get() returns (stringc value){  
        return list.get_head_data();   

    But if I need to give more character for example contains 48-bit string this won't work. [Q] How could I make this work?

  • This has worked for me so far. Not sure if it's the best way.

    function stringToBytes32(string memory source) public pure returns (bytes32 result) {
        bytes memory tempEmptyStringTest = bytes(source);
        if (tempEmptyStringTest.length == 0) {
            return 0x0;
        assembly {
            result := mload(add(source, 32))

    Also, remember that strings in solidity are UTF8 so after converting them to bytes each byte is not necessarily a character.

    EDIT: shorter version, should work the same.

    xpected elementary inline assembly operation. bytes32 result := mload(add(source, 32)) ^ contracts/Link.sol:77:22: Error: Expected token Semicolon got 'Colon' bytes32 result := mload(add(source, 32))

    @Avatar you have `bytes32 result :=` while my code is declaring result type in `returns (bytes32 result)`, just remove the `bytes32` from before `result := ...` and it should compile.

    awesome works!! Thank you sir. One final question is, if my string is larger than 32 bits such as 48 bits the remaining 12 bits will be trimmed. Would there be additional assemble line that I can obtain bits from 32 to 48? thank you for your valuable time. when I did "result := mload(add(source, 64)) " it worked!

    I tried your code in (using JavaScript VM) and entered "EOS8WE79SonHGHc5DquV4fxty9bHfnLSugCyTVXj2n9t4Uw5HL2vU" as my string, which is 56 characters long. It returned: "0x454f533857453739536f6e4847486335447175563466787479396248666e4c53" When I convert this into ASCII, I get "EOS8WE79SonHGHc5DquV4fxty9bHfnLS", which is 34 characters. Why? How do I get the original 56 characters back?

    @Curt you can't, conversion from string to bytes32 is lossy. bytes32 can contain only 32 8-bit characters but strings can be longer than 32 characters and during this operation all characters above 32 are lost. The string you pasted has 32 characters, I didn't count the quotation characters.

    Also, this function should probably be marked "internal" to save some gas

    Took me a while to figure out so for the interested: AFAIK 'source' within assembly in this context refers to the memory address of source not the string it points to. Then we add 32 to this address as the first 32 bytes are seemingly reserved to hold the number of chars in the string (/length of the array).

    Could you guys explain why this code works? What's the significance of adding 32 to the string's memory address pointer, and how does this convert it to bytes32?

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM