4

So far I am using this solidity code

pragma solidity 0.4.24;

contract Converter{
    function Converter(){

    }

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

Is there a website or online tool to easily convert strings to bytes32?

Senju
  • 701
  • 1
  • 15
  • 28

7 Answers7

9

If you have MetaMask installed, then the following works in the browser console:

> web3.padRight(web3.fromAscii('hello'), 34)
"0x68656c6c6f0000000000000000000000"
user19510
  • 27,999
  • 2
  • 30
  • 48
  • 1
    This answer is no longer correct. From the official Metamask docs:

    For historical reasons, MetaMask injected web3@0.20.7 is deprecated. It has known security issues. It is no longer maintained by the web3.js team.

    – YulePale May 24 '21 at 13:50
4

Online GUI here for converting bytes32 to string and the other way: https://blockchangers.github.io/solidity-converter-online/

robertosnap
  • 49
  • 1
  • 2
0

Another online GUI:

Convert from a bytes32/hex into a string/number or vice-versa.

https://web3-type-converter.onbrn.com/

0

You can use the bytes32 library from npm https://npm.devtool.tech/bytes32

$ npm install bytes32

$ npx bytes32
0x0000000000000000000000000000000000000000000000000000000000000000

$ npx string
0x737472696e670000000000000000000000000000000000000000000000000000

0

I made an online utility for via versa conversion of String to Bytes32. It is client side also, so everything runs on the browser.

Online String to Bytes32 Converter

marduc812
  • 101
  • 1
0

You can use Web3 tools created by Neptune Mutual which has String to Bytes32 Converter and vice versa.

https://neptunemutual.com/web3-tools/

0

Cindercloud also had an online converter, as well as some other useful tools for interacting with ethereum and other chains.

Qkyrie
  • 562
  • 3
  • 7