Hot answers tagged

53 votes
Accepted

How to store IPFS hash using bytes32?

Your example shows storing an IPFS identity using it's alphanumeric encoding (Qm...), which is the same Base58 encoding that Bitcoin uses. However, what it's representing at its core is a number (the ...
MidnightLightning's user avatar
34 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
32 votes

How to convert a uint to bytes in Solidity?

The alternative to @eth's answer is to use assembly: function toBytes(uint256 x) returns (bytes b) { b = new bytes(32); assembly { mstore(add(b, 32), x) } } This is significantly more gas-...
Nick Johnson's user avatar
  • 8,144
31 votes
Accepted

How to convert an address to bytes in Solidity?

To be even more efficient: function toBytes(address a) public pure returns (bytes memory b){ assembly { let m := mload(0x40) a := and(a, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)...
Tjaden Hess's user avatar
  • 36.9k
30 votes
Accepted

How to pass arbitrary bytes to a function in Remix (2017)

You can pass bytes parameters in Remix or browser-solidity as the array of single bytes, for example ["0x00","0xaa", "0xff"] is equivalent to "0x00aaff" Do not know why, but the Remix IDE and browser-...
Alexey Barsuk's user avatar
25 votes
Accepted

Solidity assembly question - mstore

The explanation is quite simple: In Solidity, bytes is a dynamically-sized byte array: Variables of type bytes and string are special arrays. A bytes is similar to byte[], but it is packed ...
bordalix's user avatar
  • 918
24 votes
Accepted

In solidity, how can you set a bytes variable to empty

Use "". contract MyContract{ bytes public data = "0x3333"; bytes public empty; function clearData(){ data = ""; } } Tested using https://ethereum.github.io/browser-solidity ...
eth's user avatar
  • 85.4k
24 votes
Accepted

How can I slice bytes, strings, and arrays in Solidity?

Solidity 0.6.0 and Greater (Updated 2020) As of Solidity 0.6.0, there is array slice functionality built into Solidity. The syntax is similar to existing languages in that the array takes the ...
Shane Fontaine's user avatar
23 votes
Accepted

How to convert a uint to bytes in Solidity?

It seems there now is, since solidity version 0.4.24 you can use abi.encodePacked E.G: uint i = 0; i_bytes = abi.encodePacked(i);
PiersyP's user avatar
  • 386
17 votes

How to convert a uint to bytes in Solidity?

Here's a comparison of the gas used in the three methods by @NickJohnson, @Eth and @k26dr. I've added a constant to the function modifiers as these functions do not alter the blockchain: pragma ...
BokkyPooBah's user avatar
  • 40.3k
16 votes

How to convert an address to bytes in Solidity?

Here is my tiny one-liner for address to bytes32 conversion: bytes32(uint256(uint160(addr)) << 96); If you need bytes instead of bytes32: abi.encodePacked(addr)
k06a's user avatar
  • 3,006
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
Accepted

How to convert bytes to uint in Solidity

You can use this function to convert bytes to uint: pragma solidity ^0.4.23; contract mycontract { function bytesToUint(bytes b) public returns (uint256){ uint256 number; for(...
Jaime's user avatar
  • 8,320
10 votes
Accepted

Bytes variables are connected

This is going to take a few hops to explain. The code looks innocent enough, but there is a compiler warning about uninitialized storage pointers. The warning shouldn't be ignored. Written ...
Rob Hitchens's user avatar
10 votes
Accepted

How to check if bytes variable is empty?

You can check it via .length. It will be 0 if it is empty/uninitialised. Countries[key].length
ivicaa's user avatar
  • 7,519
10 votes
Accepted

Convert/cast an address to uint256

Solution: uint256(uint160(xAddr))
dNyrM's user avatar
  • 892
9 votes
Accepted

How to concatenate a bytes32[] array to a string?

Thanks to Piper and Chris I found a working solution for Solidity <= 0.2.1. The reason why the first two log statements return different results is, because uintN is right-aligned and bytesN is ...
Stefan's user avatar
  • 181
9 votes

How to store and load an IPFS hash as bytes32 in contract written in Solidity

Whilst I have not worked with IPFS hashes, the solidity types documentation states that you can store fixed size byte arrays up to 32 bytes. As such if you want to store the whole hash in one data ...
Thomas Clowes's user avatar
9 votes

How to convert bytes to uint in Solidity

Slicing an uint out of bytes using inline assembly function sliceUint(bytes bs, uint start) internal pure returns (uint) { require(bs.length >= start + 32, "slicing out of range"); ...
libertylocked's user avatar
8 votes

How to store IPFS hash using bytes32?

Here are some js functions for stripping and re-adding the first two bytes containing the hash function and size, suitable for web3. import bs58 from 'bs58' // Return bytes32 hex string from base58 ...
Stan James's user avatar
8 votes
Accepted

The Unicorn Mystery - Solidity fails to decode valid UTF-8 encoded bytes

Unlike some of the comments suggest, the unicorn symbol 🦄 (U+1F984) is located in the contract's symbol name. You can check this by running the following in a web3 browser's console: // taken from ...
0xcaff's user avatar
  • 2,467
7 votes

How to convert a uint to bytes in Solidity?

There are no easy ways to convert anything to bytes. Here's a function: function toBytes(uint256 x) returns (bytes b) { b = new bytes(32); for (uint i = 0; i < 32; i++) { b[i] = ...
eth's user avatar
  • 85.4k
7 votes

How to concatenate a bytes32[] array to a string?

Solidity <= 0.2.1 You can retrieve the nth byte of any bytesXX type with the following code. bytes32 v = ...; byte b = byte(bytes32(uint(v) * 2 ** (8 * n))); Solidity > 0.2.1 Starting in the ...
Piper Merriam's user avatar
7 votes
Accepted

How does keccak256 concatenate values inside a Solidity smart contract?

The arguments are expressed as bytes, left-padded with zeroes to the maximum length of the data type you've passed in, and concatenated without any kind of delimiter. In Python, given two hex-encoded ...
Edmund Edgar's user avatar
  • 16.9k
7 votes
Accepted

Why is the returndata of a function returning bytes formatted in a weird way?

The ABI encoding includes an offset to the start of data. The first 0x20 is the offset to the start of data as described in this example: https://docs.soliditylang.org/en/develop/abi-spec.html#use-of-...
eth's user avatar
  • 85.4k
7 votes
Accepted

Converting bytes memory to bytes calldata

The answer was given to me on reddit so here is the fixed contract for anyone who may need it :D : // SPDX-License-Identifier: MIT pragma solidity 0.8.0; contract Foo { // I can't change the ...
Thierry P's user avatar
  • 121
6 votes
Accepted

In solidity, how can I hardcode bytes > 32 bytes

contract C1 { function f1() returns(bytes) { bytes memory bb = "\x00\x01\x02"; // you can extend this return(bb); } } Tested using online Solidity Browser, output is: Result:...
eth's user avatar
  • 85.4k
6 votes

How to convert an address to bytes in Solidity?

There are no current shortcuts and you need to write your own function. Here's the function suggested by Solidity's author, chriseth: function toBytes(address x) returns (bytes b) { b = new ...
eth's user avatar
  • 85.4k
6 votes
Accepted

Storing hardcoded calldata in a Solidity contract

The issue is that when you use a string literal like bytes public hardcodedCallData = "...
Tjaden Hess's user avatar
  • 36.9k

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