2022 Developer Survey is open! Take survey.

Hot answers tagged

58 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 (...
user avatar
  • 78k
53 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)...
user avatar
33 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. (...
user avatar
  • 78k
30 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 ...
user avatar
  • 78k
29 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 ...
user avatar
  • 50.6k
26 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));
user avatar
  • 558
19 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 ...
user avatar
19 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 ...
user avatar
  • 17.4k
18 votes
Accepted

How many letters can bytes32 keep?

Each bytes32 can store up to 32 letters (ASCII): each character is a byte.
user avatar
  • 78k
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.
user avatar
  • 1,716
15 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);...
user avatar
  • 395
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 ...
user avatar
11 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 = ...
user avatar
  • 211
11 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(...
user avatar
  • 520
9 votes
Accepted

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

It is possible to do this with assembly: pragma solidity ^0.4.8; contract c { event trace(bytes32 x, bytes16 a, bytes16 b); function foo(bytes32 source) { bytes16[2] memory y = [...
user avatar
  • 2,896
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] != ...
user avatar
  • 8,917
8 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"
user avatar
  • 26.2k
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) & ...
user avatar
7 votes

How can I modify bytes32 result to uint?

You can simply cast a bytes32 to uint with uint(number1).
user avatar
  • 7,549
7 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 ...
user avatar
7 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))); } } ...
user avatar
7 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 ...
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 ...
user avatar
  • 33.9k
5 votes
Accepted

bytes32 to uint conversion is incorrect

72370055773322622139731865630429942408293740416025352524660990004945706024960 is 0xa000000000000000000000000000000000000000000000000000000000000000L - or 10 in hex, left shifted. Without seeing your ...
user avatar
  • 7,549
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 ...
user avatar
4 votes
Accepted

Keccak256(parameter) How to pass a string to be hashed?

try this snipet function bytes32ToString(bytes32 x) constant returns (string) { bytes memory bytesString = new bytes(32); uint charCount = 0; for (uint j = 0; j < 32; j++) { ...
user avatar
  • 17.4k
4 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));
user avatar
4 votes

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

I found a solution using inline assembly: contract cutByte32 { //"0xa9c40ddcb43ebbc83add97b8f9f361f12b19bceff2f76b68f66b5bb1812365a9" //use this as remix command function cut(bytes32 sha) ...
user avatar
  • 529
4 votes
Accepted

How to convert/decode Solidity bytes32 to Python string via web3.py

The issue here is that your variable has a fixed length of 32 bytes, which explains the zeroes in the right. You can strip the zeros in python: zbytes32 = contractInstance.functions.getzBytes32()....
user avatar
  • 7,735
4 votes

Where can I find a string to bytes32 converter online?

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

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