Skip to main content
Share Your Experience: Take the 2024 Developer Survey
67 votes
Accepted

Use string type or bytes32?

Why string instead of bytes32? Use string for arbitrary-length string (UTF-8) data that's longer than 32 bytes. Frontends can decode a long string easier using methods like web3.toAscii or UTF-8 (...
eth's user avatar
  • 85.9k
62 votes
Accepted

How to convert a string to bytes32?

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)...
Grzegorz Kapkowski's user avatar
41 votes

How to convert a bytes32 to string

As of feb 2021 you can do bytes32 foo = "hello"; string memory bar = string(abi.encodePacked(foo));
e18r's user avatar
  • 728
39 votes
Accepted

Why do Solidity examples use bytes32 type instead of string?

2 main reasons: Contracts currently cannot read a string that's returned by another contract. The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (...
eth's user avatar
  • 85.9k
35 votes
Accepted

What is the difference between bytes and bytes32?

Bytes is a dynamic array of bytes. It's shorthand for byte[] and you'll see examples of a bytes being treated as an array in code from time to time. myByte[x]. It can have a length of zero and you can ...
Rob Hitchens's user avatar
  • 55.3k
31 votes

How to convert a bytes32 to string

Here's one: function bytes32ToString(bytes32 x) constant returns (string) { bytes memory bytesString = new bytes(32); uint charCount = 0; for (uint j = 0; j < 32; j++) { byte ...
eth's user avatar
  • 85.9k
26 votes

Use string type or bytes32?

Like the other post said, you only want to use strings for dynamically allocated data, otherwise Byte32 is going to perform better. Bytes32 is also going to be better in gas. If you want to play ...
Matthew Campbell's user avatar
25 votes
Accepted

How to convert a bytes32 to string

Based on the latest compiler version 0.4.24, I use the following. function convertingToString()public returns(string){ bytes32 memory hw = "Hello World"; string memory converted = string(hw);...
R.D's user avatar
  • 505
23 votes

How to convert a string to bytes32?

ethers.js has a built in utility function for this: https://docs.ethers.io/ethers.js/html/api-utils.html#bytes32-strings const ethers = require('ethers') const utils = ethers.utils const inBytes = ...
bornSwift's user avatar
  • 339
23 votes

How to convert a string to bytes32?

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 ...
Badr Bellaj's user avatar
  • 18.8k
21 votes
Accepted

How many letters can bytes32 keep?

Each bytes32 can store up to 32 letters (ASCII): each character is a byte.
eth's user avatar
  • 85.9k
18 votes
Accepted

How to convert a uint256 type integer into a bytes32?

Just use bytes32(u). It's as easy as that because they are both 2^256 (unlike bytes). No need for a separate function, but to clarify: function convert(uint256 n) returns (bytes32) { return ...
eth's user avatar
  • 85.9k
15 votes
Accepted

Convert with JS a Solidity bytes32 to a Solidity string

Try using the web3.js function web3.toAscii (doc), which might change name to web3.toUtf8 or something similar in the future.
MrChico's user avatar
  • 1,786
12 votes

How to convert a bytes32 to string

How to convert a bytes32 to string: pragma solidity ^0.4.15; contract Bytes32ToString { function bytes32ToStr(bytes32 _bytes32) public pure returns (string) { // string memory str = string(...
Viktor's user avatar
  • 550
11 votes
Accepted

How to get string from hex on Smart Contracts results

You can use web3.toAscii(hexString) as documented in Web3 JavaScript Ðapp API: > web3.toAscii("0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420") "MarketPay.io is deploying Smart ...
BokkyPooBah's user avatar
  • 40.3k
11 votes
Accepted

How to compare two bytes32?

A comparison between two bytes32 IS possible in solidity. Consider this simple contract: pragma solidity 0.5.4; contract Test { bytes32 public constant bytes32_ = "Hello World!"; bytes32 ...
Daniel Portugal's user avatar
10 votes
Accepted

How can I split a Bytes32 source into two halfs and store them in Bytes16?

This code is no longer correct on modern versions of Solidity. Please see Sergey Kaunov's answer. It is possible to do this with assembly: pragma solidity ^0.4.8; contract c { event trace(...
max taldykin's user avatar
  • 2,966
10 votes

Convert bytes32 to address

For solidity 0.5.x you can use pragma solidity ^0.5.0; contract Test { function test(bytes32 data) external pure returns (address) { return address(uint160(uint256(data))); } } ...
Alfredo Egaf's user avatar
9 votes

Why do Solidity examples use bytes32 type instead of string?

I have test in this site https://ethfiddle.com/zLxE5Y-8B4 contract TestGas { string constant statictext = "Hello World"; bytes11 constant byteText11 = "Hello World"; bytes32 constant ...
Xat_MassacrE's user avatar
9 votes
Accepted

How to check if a bytes32 represents an empty string?

The bytes32 type is always exactly 32 bytes. Therefore, its length is always 32. It is unaware of whether it contains a string, number or something else. I would recommend doing: require(_name[0] != ...
Jesbus's user avatar
  • 10.5k
9 votes
Accepted

Where can I find a string to bytes32 converter online?

If you have MetaMask installed, then the following works in the browser console: > web3.padRight(web3.fromAscii('hello'), 34) "0x68656c6c6f0000000000000000000000"
user19510's user avatar
  • 28.1k
8 votes

How to convert a string to bytes32?

Assembly is not needed in version 0.8.7 string text = "whatever"; bytes32 stringInBytes32 = bytes32(bytes(text));
Jose4Linux's user avatar
8 votes

How can I modify bytes32 result to uint?

You can simply cast a bytes32 to uint with uint(number1).
Nick Johnson's user avatar
  • 8,174
7 votes
Accepted

How can I modify bytes32 result to uint?

Or just do away with your number1 variable completely, while also remembering to change the return type: function getTest1() constant returns (uint) { return uint(lastblockhashused) & ...
Richard Horrocks's user avatar
6 votes
Accepted

bytes - quotes or not?

Always use quotes. JavaScript has a maximum integer precision of 64 bits. This means that if your number is over 8 bytes, and you type it in without quotes, you will immediately lose precision. For ...
Tjaden Hess's user avatar
  • 37.1k
6 votes
Accepted

bytes32 to uint conversion is incorrect

72370055773322622139731865630429942408293740416025352524660990004945706024960 is 0xa000000000000000000000000000000000000000000000000000000000000000L - or 10 in hex, left shifted. Without seeing your ...
Nick Johnson's user avatar
  • 8,174
6 votes

How do I check if a bytes32 variable in a mapping has been assigned value?

In Solidity mappings, all possible keys are assigned 0 by default. So if a valid value in your use case is always non-zero, then you can check with: addressName[addr] != 0 And if 0 is a valid value,...
Bastien's user avatar
  • 376
6 votes

What does add(source, 32) do in solidity?

To better understand it is better to include part of the function function stringToBytes32(string memory source) public pure returns (bytes32 result) { // .. assembly { result := ...
Ismael's user avatar
  • 30.5k
6 votes
Accepted

Convert number to bytes in ethersjs?

ethers also has a padding function where it will add the zeros to make it the correct length. It looks like this const oracleResponse = ethers.utils.hexZeroPad(ethers.utils.hexlify(1), 32) This will ...
Patrick Collins's user avatar
5 votes
Accepted

Convert bytes32 to ordered bytes2, bytes4, etc

Your question is not clear enough. When you want to split 32 bytes into 5 variables that have a total length of 2+2+2+2+8+1 = 17 you should specify what bytes you want to be chunked (bytes from left ...
Alexey Barsuk's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible