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
0 votes
1 answer
318 views

encoding several variables into a byte

I want to define a byte256 and I want to assign different sections of the byte to different variables, any literature or guidance is appreciated. for further clarity I want to do something like ...
0 votes
1 answer
482 views

Encode Struct[] in ethers to pass to a dynamic bytes

I have a method in solidity: function getVotesWithParams( address account, uint256 blockNumber, bytes memory params ) public view virtual override returns (uint256) { return _getVotes(...
1 vote
1 answer
137 views

Grab first 20 bytes of 32-bytes input

In solidity casting or assembly, how do I grab the first 20 bytes of this 32-bytes input: 0x00010000c900c90062fceaad00000000000000000002af416812f9bb6f2fdff5? Thanks!
0 votes
1 answer
573 views

How to encode string as bytes array?

A similar question has been asked but using web3 in java, however, I would like to encode a string to bytes[] in solidity itself.
0 votes
1 answer
44 views

Solidity: How to decode / trace / search custom error byte code

I ran into this Custom Error during my Foundry test: [FAIL. Reason: Custom Error cd786059:(0xF628...)] and I had no idea about what it means... then I figured out where it went wrong after tracing ...
1 vote
2 answers
667 views

How to convert string to bytes256 or arbitrary bytes?

I already know how to convert bytes32ToString and stringToBytes32: function bytes32ToString(bytes32 x) public pure returns (string memory) { bytes memory ...
1 vote
1 answer
94 views

How to decode a bytes value from Ethereum event with ethers.js

I've got the following event declaration in my Solidity contract: event RemoteOrderCreated(bytes32 indexed id, uint32 indexed nonce, address indexed sender, bytes input); And I capture the event ...
3 votes
1 answer
2k views

How to pass bytes parameter to contract method call using Web3.js?

I'm trying to call a method in a contract and the method is like this: function myMethod(uint8 tid, bytes calldata _bytes) public view returns (string memory) { ... } I'm using Web3.js to ...
1 vote
1 answer
844 views

Solidity: How to represent bytes32 as string

This may be simple in other languages but I can't figure out how to do it in Solidity. I have a bytes32 like this 0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712. I don't want to ...
1 vote
1 answer
2k views

Solidity Bytes Comparison

I was going through the Create Your Own CryptoCurrency documentation and got stuck at the proofOfWork section. In this, they try to do a check i.e. require(n >= bytes(difficulty)) //Check if its ...
0 votes
2 answers
1k views

Store literal bytes4 as string

Does anyone have the 'somefunc' below to store bytes (specifically bytes4) as a string? Ideally something to get method_s in the code below: contract Test{ bytes4 public method; string public ...
0 votes
0 answers
33 views

Why "It's better to use the bytes type instead of the bytes1[] type"? Solidity

I read a Note in documentation of Solidity: The type bytes1[] is an array of bytes, but due to padding rules, it wastes 31 bytes of space for each element (except in storage). It is better to use the ...
1 vote
1 answer
288 views

Byte32 error, even after converting string to hex

I looked everywhere for people with similar problems, but I couldn't find anything. I am confused why I keep getting this error (yes, I know others have had this error too, but not for the same ...
0 votes
0 answers
252 views

Uniswap v3: How do I get the path bytes from solidity from array function parameters?

I am trying to write a smart contract that can facilitate multihop swaps. It is important that I am able to pass in 2 arrays into the swap function: an address[] of the tokens and uint256[] of the ...
5 votes
2 answers
10k 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"...
0 votes
1 answer
99 views

Simple contract fails to deploy to Sepolia

I am trying to deploy a very simple contract to Sepolia. Here is the code: pragma solidity ^0.8.18; contract Object { bytes32 private UID; bytes private Desc; constructor(...
2 votes
1 answer
2k views

what is the size of the web3.eth.sign() result?

I am looking for a solution to emit a dynamique array of signature using event. I thought that signature is a byte32 and it works, but I think is a 65 bytes. so what is the exact size and how could ...
2 votes
1 answer
416 views

Solidity (find a word in a string and replace it)

I am learning solidity string/byte manipulation and I am trying to create a Solidity function that finds specific words in a string/format and replaces them with given variables to return "edited&...
5 votes
4 answers
5k 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]: ...
7 votes
1 answer
642 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 ...
0 votes
1 answer
161 views

How to encode arbitrary amount of parameters for encodeWithSignature without adding them to function definition?

Lets says we have Contract A that needs to call Contract B at a certain address which the requirements that Contract A doesn't know the exact parameter structure the function in that Contract B needs &...
0 votes
0 answers
168 views

Calculate the contract address created using create2 with dynamic byte string as a salt

EIP-1014 describes salt as bytes32. However, the function create2 written in assembly allows passing byte string of arbitrary size as the salt parameter. Example function deploy(bytes memory bytecode, ...
0 votes
1 answer
83 views

Chainlink Functions return an array or list of strings from Functions-request-source.js

Hope someone can help. I am stuck trying to work out how best to extract the different string values from the returned bytes data. Chainlink Function request source requires the return value to be a ...
0 votes
1 answer
29 views

Unintentional extra arguments on calldata

I manually inserted 2 32 bytes arguments to a function flipSwitch(bytes memory someData) whose function identifier happens to be 0x30c13ade. My manual arguments: ...
4 votes
2 answers
1k 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 ...
0 votes
1 answer
30 views

mapping not showing any value corresponding to string key(converted from bytes32)

I have bytes32 data which needs to be converted into a string, then use to get value from mapping(string => string) Trying to convert bytes32 to string using : string(abi.encodePacked(temp)) It is ...
0 votes
0 answers
13 views

Expected Primary Expression error on signature function

With this isValidSignature function: // NOTE: This method not safe, contract may attack by signature replay. /** * Verify if the signer has a valid signature as claimed * @param signer ...
1 vote
1 answer
117 views

Encode String to byte[] to invoke method via web3j

I have a method in a contract that receives a bytes object. Web3J generated code in Java translates it to a byte[] object as parameters. I have the correct String "0x....." thet represents ...
2 votes
4 answers
87 views

How to batch list into a single bytes object?

Let's say I have a string list of parameters ["Example String", "vitalikbuterin", "1000"] How can I encode this list into a bytes object, and then decode the bytes to get ...
3 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 =...
1 vote
1 answer
29 views

Can someone explain to me how returns work under the hood in solidity?

Can someone explain how I will be successfully able to return this function reddish(uint256 _red) public view returns (bytes memory) { bytes memory err = abi.encode(msg.sender, _red); assembly ...
3 votes
2 answers
774 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?...
1 vote
1 answer
123 views

How exactly does the 'hex' prefix work for bytes?

I found some code snippets using the 'hex' prefix before a string in a bytes variable. I can't seem to find its description in the solidity docs. bytes memory bytesVar = "0x01"; bytes ...
3 votes
2 answers
3k 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, ...
0 votes
2 answers
277 views

Convert bytes20 to address in assembly

Can you convert a bytes20 to address in assembly (not solidity-typecasting)? If so, how?
1 vote
1 answer
1k 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 ...
0 votes
1 answer
55 views

Grab the last 32 bytes of 64-bytes input

In assembly, how do I grab the last 32 bytes in a 64-bytes input? I know that this code is for grabbing the first 32: bytes32 half; assembly { half := mload(add(accData2, 32)) }
0 votes
1 answer
147 views

TypeError: Member "encodeCall" not found or not visible after argument-dependent lookup in abi

I am trying to test abi.encodeCall on Remix. I have this function function getTestData() external pure returns (bytes memory){ // passing empty args () return abi.encodeCall(TargetContract.send,()); } ...
0 votes
1 answer
51 views

How to pass bytes4 to mapping?

I'm trying to pass bytes4 type to mapping but all I have got is an error saying: **ParserError: Expected identifier but got '='** bytes4 example = 0x11030330; mapping(bytes4 => address) facets; ...
0 votes
2 answers
964 views

How to use initialize a bytes array with abi data?

To initialize a bytes array with the bool value True encoded as ABI data, I'm attempting to do it like this: bytes memory arr = '0x0000000000000000000000000000000000000000000000000000000000000001'; ...
1 vote
0 answers
67 views

How to handle both String and Bytes return types in Rust

I'm trying to query the MAKER ERC20 contract and get the name and symbol of the token. The problem is that they are stored as a Bytes and I don't want to change the return type of my function from a ...
12 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 ...
1 vote
1 answer
1k views

Unable to deduce common type for array elements in bytes[ ]. Why I am getting this even the first line works perfectly but 2nd one's throws error?

I am trying to get understanding of bytes array but encountered an error where, contract Mytest { > > bytes[] public arr1 = [bytes("happy"), "names"]; // This line ...
0 votes
2 answers
96 views

bytes5 vs string

I have an smart contract with some defined fixed length variables from 3 to 5 characters long. Defining these variables as string is more intuitive for those that come from another languages. Also ...
2 votes
1 answer
94 views

How to reverse a Solidity bytes sorting?

Suppose I have this bytes pad: 0x0123456789abcdef How can I make it like this: 0xfedcba9876543210 Just by Solidity and not by Assembly.
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 ...
0 votes
1 answer
52 views

get uint value inside a bytes32 slice

I have the following code: function test_bytes(bytes32 test) external pure returns(bytes3 idx,uint256 idx2) { //test = ...
1 vote
0 answers
446 views

Escaping HTML special characters in Solidity

I realize I am not supposed to do this, and yet I am curious about the least-bad approach. Will the code below work? It is based on some of Uniswap's code. function escapeHTML(string memory input) ...
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 ...
2 votes
1 answer
692 views

Multiple matching functions error when doing interface.encodeFunctionData

I have openzeppelin's standard ERC721 contract. Below is the helper function that returns bytes data that will be used for some purpose. async function getErc721SafeTransferFromTransaction(...