28

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?

41

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

function stringToBytes32(string memory source) 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)) – alper Oct 10 '16 at 11:30
  • @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. – Grzegorz Kapkowski Oct 10 '16 at 12:42
  • 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! – alper Oct 10 '16 at 15:26
  • I tried your code in remix.ethereum.org (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 Aug 16 '17 at 21:14
  • @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. – Grzegorz Kapkowski Aug 18 '17 at 7:23
15

string is not equal to bytes32 but it is equal to bytes, because its length is dynamic.

so you could use a casting bytes B=bytes(S); //S string

E.g

contract string_test {

    function string_tobytes( string s) constant returns (bytes){
        bytes memory b3 = bytes(s);
        return b3;
    }
}

The conversion of string to bytes32 is possible only using assembly

  • Error: Explicit type conversion not allowed. bytes b = bytes("a"); – alper Oct 9 '16 at 17:35
  • try the new code above using memory variable. – Badr Bellaj Oct 9 '16 at 20:24
  • TypeError: Explicit type conversion not allowed from "literal_string "mined"" to "bytes storage pointer". bytes memory data = bytes("mined"); – bitsanity Oct 9 '17 at 16:35
0

Please reference this. You can convert string to bytes32 array.The length of string can be longer than 32 .

https://ethereum.stackexchange.com/a/13388/6353

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.