Questions tagged [bytes]

both the fixed-size array byte, bytes1...bytes32, and the dynamic array bytes types in Solidity

Filter by
Sorted by
Tagged with
40 votes
2 answers
32k views

What is the difference between bytes and bytes32?

I know bytes32 stores 32 bytes, but what does bytes store? And what is the difference between the two?
cryptorenaisance's user avatar
39 votes
7 answers
32k views

How to convert a uint to bytes in Solidity?

Is there an easy way to convert a uint to bytes in Solidity?
eth's user avatar
  • 85.7k
34 votes
7 answers
16k views

How to store IPFS hash using bytes32?

Following Q/A (What datatype should I use for an IPFS address hash?) recommend us to use bytes to store IPFS hash. I was using following example(https://github.com/AdrianClv/ethereum-ipfs/blob/master/...
alper's user avatar
  • 8,395
28 votes
2 answers
28k views

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

According to this question I could write a simple contract like: contract SimpleStorage { bytes input; function setInput(bytes enterBytes){ input = enterBytes; } } Using "0x1234" as a ...
floyd's user avatar
  • 599
22 votes
2 answers
20k views

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

I tried this contract MyContract{ bytes public data = 0x3333; function clearData(){ data = 0x; } } But solc doesn't like me trying to set data to 0x
Akhil F's user avatar
  • 1,928
21 votes
6 answers
27k views

How to convert an address to bytes in Solidity?

What is the recommended way to convert an address to bytes in Solidity?
eth's user avatar
  • 85.7k
20 votes
2 answers
3k views

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

The Ethereum Foundation has the Unicorn token to encourage donations, and the unicorn emoji is represented by three bytes. How in the world did they do that? I know that Solidity supports unicode ...
user avatar
17 votes
2 answers
8k views

Convert struct to bytes in Solidity

Is there any convenient way to convert (e.g. cast) a struct type to bytes? I'm trying to call a method like the one below (taken from here) where you can pass some (arbitrary) contextual info that ...
jlpiedrahita's user avatar
15 votes
5 answers
14k views

How to convert bytes to uint in Solidity

Hey is there a way to convert bytes to uint in Solidity?
arete's user avatar
  • 599
13 votes
1 answer
16k views

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

Is it possible to slice variables in Solidity?
Shane Fontaine's user avatar
13 votes
4 answers
5k views

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

I want to split a 32 Byte source: first half in Bytes16 half1; second half in Bytes16 half2. My code works, however only for dynamic array, not with fixed size 16. pragma solidity ^0.4.8; contract ...
floyd's user avatar
  • 599
11 votes
1 answer
5k views

Error when using string type as a mapping key

The following code doesn't compile, because compiler needs a mapping key to be of fixed-size type in order to create accessor for a public mapping. I'm using here string type. My strings are 24 byte ...
manidos's user avatar
  • 4,298
9 votes
2 answers
8k views

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

I am passing an array of bytes32 to my function. This array can contain numbers or hashes etc. to identify an object. In a new use case I want to pass a URL to this function by splitting the URL into ...
Stefan's user avatar
  • 181
9 votes
2 answers
10k views

Solidity Invalid implicit conversion from string memory to bytes memory requested

pragma solidity ^0.5 contract ProofOfExistence { // ... some code here function proofFor(string memory document) public view returns(bytes32) { return sha256(document); } //...
Khalid168's user avatar
8 votes
1 answer
7k views

Solidity assembly question - mstore

I was searching on how to convert a uint to bytes in Solidity and found this answer. function toBytes(uint256 x) returns (bytes b) { b = new bytes(32); assembly { mstore(add(b, 32), x) } } ...
Pani's user avatar
  • 280
8 votes
1 answer
7k views

How to pass arbitrary bytes to a function (2016)

I'm trying to implement a contract which will store bytes blob, a dynamic bytes array. For example I have following struct: Sign { address signer; bytes16 signType; bytes sign; } plus ...
Igor Artamonov's user avatar
7 votes
2 answers
7k views

How to compare two bytes32?

I was using strings before I read that using bytes is gas cheaper. Now I changed my strings to bytes32 but I don't know how to compare them. I need to check if two bytes32 are equal in the require ...
Radek_pl's user avatar
  • 791
7 votes
1 answer
698 views

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

Solidity 0.4.25. Consider this code contract A{ function test() public view returns (bytes){ bytes memory output = abi.encodePacked(bytes32(1)); return output; } } The ...
JBrouwer's user avatar
  • 1,062
6 votes
1 answer
4k views

Convert/cast an address to uint256

Following these details on the values that make part of the calldata that goes into an L2 message in Arbitrum: https://developer.offchainlabs.com/docs/arbos_formats#l2-messages How do you convert/...
dNyrM's user avatar
  • 904
6 votes
1 answer
8k views

How to check if bytes variable is empty?

I have a mapping in my smart contract with bytes32 keys and bytes values. How can I check if bytes value exists for specific key? Mapping is something like this: mapping(bytes32 => bytes) public ...
saman.shahmohamadi's user avatar
6 votes
1 answer
2k views

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

An IPFS hash can be expressed in hexadecimal which is 34 bytes long. Afaik it is most efficient to store fixed length values in a Ethereum contract, thus a maximum of 32 bytes (bytes32). Because the ...
bumi's user avatar
  • 348
6 votes
2 answers
7k views

Concat two bytes arrays with Assembly

Does anyone know how to efficiently concat two bytes arrays with assembly language to save gas costs? Currently I have function mergeBytes(bytes param1, bytes param2) returns (bytes) { bytes ...
Tomas Gobionis's user avatar
6 votes
1 answer
2k views

In solidity, how can I hardcode bytes > 32 bytes

I want to create a contract with a hardcoded bytes variable. contract Test(){ function Test(){ bytes x = 0x..... //greater than 32 bytes } } The above returns a Type int_const not ...
Akhil F's user avatar
  • 1,928
5 votes
2 answers
2k views

How to load the first 4 bytes from a bytes calldata var?

It is common for calldata to be encoded as bytes calldata when developing contracts in Solidity. I need to extract the function selector from such a bytes var. What's the best way to load the first 4 ...
Paul Razvan Berg's user avatar
5 votes
4 answers
6k views

Decode multicall bytes[] into readable format?

I created a Uniswap V3 pool using UI. In Transaction multicall(bytes[] data) function was called with this parameter: [0]: 0000000000000000000000000000000000000000000000000000000000000020 [1]: ...
MantasFam's user avatar
  • 327
5 votes
1 answer
624 views

Bytes variables are connected

Why are these variables confounding? The returned result shows that both loops are effecting both bytes variables (second returns the same result). function mix() public returns (bytes) { ...
0TTT0's user avatar
  • 672
5 votes
2 answers
8k views

Converting bytes memory to bytes calldata

I am getting the following error: Error: Type bytes memory is not implicitly convertible to expected type bytes calldata. So I was wondering if it was possible to convert from bytes memory to bytes ...
Thierry P's user avatar
  • 121
5 votes
2 answers
11k views

Solidity convert hex string to bytes

I try to convert hex string to bytes. function MyContract() public { string memory str = "A76A95918C39eE40d4a43CFAF19C35050E32E271"; array1 = bytes(str); bytes memory str1 = hex"...
fragnatic's user avatar
5 votes
2 answers
9k views

passing String object with "0x.." value as bytes32 to a solidity function

I'm having trouble to pass bytes32 to a solidity function from javascript. It's converting to an incorrect bytes32 in solidity. The javascript is a test, running with truffle v3.3.1 on testRPC v3.0.5....
szerte's user avatar
  • 1,231
5 votes
1 answer
954 views

Storing hardcoded calldata in a Solidity contract

I've been trying to store a hardcoded call in a contract, but I'm getting confused on the encoding. Let's say I have the following bytes: ...
Simon de la Rouviere's user avatar
5 votes
2 answers
7k views

How do I check in solidity if interface is supported?

In the new ERC721 documents (erc721.org), the interface says that one of the functions should be: /** * @dev Returns `true` if the contract implements `interfaceID` and`interfaceID` ...
thefett's user avatar
  • 3,873
5 votes
1 answer
2k views

How do you get a string value from msg.data in the fallback function?

Im trying to do what The DAO did and do something when a sender sent money to my contract address. But im not sure how to. The something I want to do is to just create a new user in a mapping with the ...
vonGohren's user avatar
  • 211
5 votes
1 answer
7k views

How to return bytes array in Solidity Contract

I'd like to input & output an array of bytes (of approximately 56 characters each). The input works but the compiler gives a TypeError on bytes[] for the output function: TypeError: Internal type ...
Curt's user avatar
  • 307
5 votes
3 answers
3k views

Converting from bytes in 0.5.x?

I'd like to convert bytes into any other type. I.e. "0x57D80C61128d608857d5310BB514223Bb6011CAB" into the address 0x57D80C61128d608857d5310BB514223Bb6011CAB, "5" into the uint256 5 and so on. I have ...
FlashyQpt's user avatar
4 votes
2 answers
5k views

Convert bytes to hexadecimal string in solidity

In a smart contract I have stored a bytes4 value: 0xa22cb465. I'd like to parse this value as a string: string memory test = "0xa22cb465" I've only stumbled upon explanations on how to ...
Riccardo Perego's user avatar
4 votes
1 answer
2k views

How does keccak256 concatenate values inside a Solidity smart contract?

I'm developing a smart contract in Solidity in which I need to evaluate the hash of two concatenated bytes32. I don't have to concatenate the bytes32 since the sha3 function supports more than one ...
gatb27's user avatar
  • 350
4 votes
1 answer
887 views

Read more than 32 bytes from memory (Assembly)

Assume I am passing a variable bytes (memory) to a function. This variable corresponds to A concatenated with B where A is of size 32 and B is 96 bytes, how can I return A (bytes32) and B (bytes). ...
Jaime's user avatar
  • 8,340
4 votes
1 answer
6k views

What does the bytes format “\xFF\xFF\xFF” mean and how it is interpreted?

bytes sig = hex"aca7da" and bytes sig = "\xac\xa7\xda" seem to be the same thing, ASCII interpreted as hex. Does the format "\xFF\xFF\xFF" somehow code that it should be read as hex?
cryptorenaisance's user avatar
4 votes
2 answers
4k views

Encode a struct with solidity

Is it possible to encode a struct in Solidity without serializing each variable separately? Something like: struct MyStruct { uint x; uint y; } MyStruct storage myStruct = MyStruct({ x: 11, ...
dNyrM's user avatar
  • 904
4 votes
1 answer
1k views

How do you handle the hex prefix without the value (only "0x")?

First, I'm caling function foo() (from another contract) that returns me a uint in the form of bytes. Then, I'm using BytesLib.toUint(foo(), 0) to retrieve the uint. When the value is supposed to be 1,...
António Gonçalves's user avatar
4 votes
2 answers
1k views

How to convert `bytes4` to `bytes memory` in Solidity?

bytes4 is a commonly needed data type in Solidity, due to its use in msg.sig and, more broadly speaking, in function selectors (e.g. Contract.func.selector). How can one convert bytes4 to bytes memory?...
Paul Razvan Berg's user avatar
4 votes
2 answers
2k views

How to extract n bytes from a bytes data?

I want to exract n bytes from a given bytes data. //this function extract the first uint8 from a byte data function extractSelector (bytes data) public constant returns (uint8) { uint8 sel =...
maroodb's user avatar
  • 1,121
4 votes
1 answer
373 views

Parse arbitrary bytes input

A function recives bytes as input: function foo (bytes data) How do I parse that into individual byte32 chunks? function foo (bytes data) returns (bytes32[]) My goal is to understand how to handle ...
user2241566's user avatar
4 votes
0 answers
2k views

Call method with bytes argument from Truffle

When calling a Smart Contract method that requires a bytes parameter, truffle is not accepting the bytes parameter, no matter how I express it. All these tests fail on truffle: // ...
Juan Ignacio Pérez Sacristán's user avatar
3 votes
2 answers
2k views

How to decode `encodePacked` data

I have two addresses, _token1 and _token2. I would like to pack them into bytes data to pass it to a function. bytes memory params = abi.encode(_token1, _token2); The obtained params is the ...
ywat's user avatar
  • 217
3 votes
2 answers
2k views

How to decode the output of an eth_call that returns a Tuple[]

I am developing a low-level library that interacts with RPC nodes directly. There is a requirement to call the AaveV2 getAllReserveTokens function, and I want to decode the output data. The particular ...
pestopasta's user avatar
3 votes
1 answer
4k views

Is it safe to use abi.encodePacked in production solidity code?

In my solidity game project, I am using this: if (king == keccak256(abi.encodePacked(player)){ //continue as king } else{ // not king } Where player data type is bytes and king is a player ...
Adshead's user avatar
  • 31
3 votes
1 answer
3k views

Convert bytes32 to ordered bytes2, bytes4, etc

For example, I need to slice my bytes32 variable to chunks, and put result into 5 variables, saving the order, bytes2, bytes2, bytes2, bytes2, bytes8, bytes1 (aka byte), the order should be saved and ...
yanik's user avatar
  • 473
3 votes
1 answer
936 views

Why can't we do bitwise operations on Bools?

So I'm running an if/then comparator function with bools. It's essentially and xor function but when I tried a ^ b i get operator not compatible with types bool and bool. Given that bools (in ...
o0ragman0o's user avatar
  • 4,320
3 votes
1 answer
3k views

How to convert bytes32 to uint8 in solidity ^0.5.0?

I can see that conversions from bytes to uint used to be much easier in previous versions of solidity. Now, using the same syntax, I get this error: TypeError: Explicit type conversion not allowed ...
Paul Razvan Berg's user avatar

1
2 3 4 5