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
0 answers
8 views

Is 1 word 4 bytes or 32 bytes in solidity?

I'm confused about the term word in solidity. When I google word, it says 1 word = 2 bytes but whenever I read word mentioned in the context of solidity, it sounds like 1 word = 32 bytes? What is a ...
user avatar
  • 255
0 votes
0 answers
19 views

How to convert address to bytes in solidity? [duplicate]

I tried with this function, but it's not working, getting the same address input in output. function toBytes(address a) public pure returns (bytes memory b){ assembly { let m := mload(0x40) a ...
user avatar
0 votes
1 answer
41 views

Return bytes from inline assembly

I have two little tests in which I'm trying to return bytes that are populated via inline assembly. It works when I'm using a pre-defined return variable, but I'm getting a buffer overrun when I try ...
user avatar
  • 3
0 votes
1 answer
50 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.
user avatar
  • 230
0 votes
0 answers
39 views

Python bytes not being read into solidity bytes

I have this code for returning bytes from a python script: blockbounds is a list of integers component.length and component.color are both integers def buildBytes(): componentsToBytes = bytearray()...
user avatar
1 vote
1 answer
156 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, ...
user avatar
  • 307
0 votes
0 answers
12 views

Modify msg.data

Is it possible to modify msg.data by assigning a new value to it and forwarding it to the next function call? For example: Proxy.sol fallback() external payable virtual { _fallback(); } ...
user avatar
  • 307
0 votes
1 answer
209 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 ...
user avatar
0 votes
0 answers
21 views

Generate L2 message in Arbitrum

One of the ways of sending an L1 message to L2 in Arbitrum is through the Inbox contract and its sendL2Message(bytes messageData) function. https://developer.offchainlabs.com/docs/sol_contract_docs/...
user avatar
  • 307
0 votes
1 answer
255 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/...
user avatar
  • 307
0 votes
2 answers
271 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 ...
user avatar
1 vote
1 answer
603 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]: ...
user avatar
  • 122
1 vote
1 answer
76 views

sload. with struct - loading bytes

I'm trying to load varios bytes from a struct in assembly. I found a good explanation here for uint. While this is working for uint and the whole memory slot i cant figure out how to get a bytes1 or ...
user avatar
0 votes
1 answer
78 views

How can i change the 5th and 6th byte from bytes8

Hi Guys , How can i change the 5th and 6th byte from this bytes8 0x3FcB875f"0000"ddC4,or just arrive at this solution where: last four bytes(of bytes8) are this way 0x********"0000ddC4&...
user avatar
0 votes
0 answers
75 views

How to remove the last element of a string in Solidity

I am trying to get better at strings and bytes manipulation. But the general lack of built-in functions is making my life much harder than it should. Basically, I tried to write a solidity function ...
user avatar
  • 636
0 votes
0 answers
101 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&...
user avatar
  • 636
1 vote
1 answer
220 views

How does Bytes-Utils slice function work?

I'm quite confused of how the bytes-utils library's slice function. I have a few questions about how it works, especially about the code below. The full code can be found here. Why is tempBytes ...
user avatar
  • 13
0 votes
1 answer
129 views

EXTCODESIZE returning 32 for contract size less than 32

I'm currently completing the Ethernaut challenges, one of them is to deploy a small contract (runtime bytecode is 10 bytes long). I'm running the tests on my local machine, and using Solidity to ...
user avatar
0 votes
1 answer
669 views

How to send bytes data to contract with web3.py to decode

I'm doing a test and trying to send bytes data to a contract so it can parse, store in variables, and return a value. Solidity code pragma solidity 0.6.12; contract testbyte { function sendBytes(...
user avatar
0 votes
1 answer
31 views

Store addresses in bytes variable

I need help with storing a bytes value. contract StoreBytes { bytes public addresses; function addMe() external { // TODO Add msg.sender to addresses. } } What I want is that msg....
user avatar
  • 633
0 votes
0 answers
38 views

Decode bytes32 with multiple arguments

I found a Chainlink Adapter that fits my goals and it returns several arguments separated by : in bytes32 format. eg. (fin:win:2:1651:VP) => ...
user avatar
0 votes
1 answer
32 views

Requiring a bytes32 mapping to be empty, why does my transaction fail?

I have a contract here: contract source code It's a simple one that allows users to create new key:value mappings with bytes32 variables, as well as update the value of a key given that: the key is ...
user avatar
0 votes
0 answers
77 views

Why We use hex'ff' for genrate the pair address in uniswapV2 library?

I am working on uniswap router contract for _addLiquadity and there uniswap used uniswapV2library and into that library this below function is return the pair address but i dont understand what first ...
user avatar
0 votes
0 answers
174 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) ...
user avatar
  • 123
1 vote
1 answer
77 views

Converting bytes16 to int128

I am working on a little decentralized game with a world made of plots. It has a map which links a location to a Plot struct. A location is saved as a bytes16 but represents two int64 (coordinates can ...
user avatar
  • 205
0 votes
0 answers
12 views

Sending arbitrary bytes to a function for later unpacking

I have a function I want to write that takes the smallest possible input data as an exercise: function test(bytes memory test_data ) public { // the bytes memory is 53 bytes, 16 + 16 + 20 +...
user avatar
1 vote
2 answers
443 views

Which of abi.encodePacked and abi.encode was standard in Solidity compiler before Solidity 0.5?

Before Solidity v0.5, keccak256() took multiple arguments, and after v0.5 it requires a single bytes argument. Which of the two encoding types, encode/encodePacked, did the compiler use?
user avatar
  • 23
0 votes
1 answer
223 views

How do I check for empty bytes array?

In 0.7.6 I want to check if a bytes array is equal to "" or not, but the compiler doesn't like bytes fingerprint = ""; if(fingerprint!=""){ // do something } How do I ...
user avatar
  • 51
2 votes
1 answer
338 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 ...
user avatar
0 votes
1 answer
48 views

What is the most gas-efficient way to hard-code a bytes array?

I need to hardcode some multi-Kb bytes into my smart contract. So far, the best way I have found to do this is to use abi.encodePacked... return abi.encodePacked( abi.encodePacked( ...
user avatar
0 votes
1 answer
35 views

String to Bytes Converts to Incorrect Value

I have a string array stored in memory as https://cloudflare-ipfs.com/ipfs/QmSXDsFeNaPa3CJKmn8WKBnA421Zv5r3Ra8n71LZhvEi9s This should convert to ...
user avatar
  • 113
1 vote
0 answers
18 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 ...
user avatar
  • 11
0 votes
1 answer
2k views

Decode raw data within Solidity

If I only have the raw input data of a contract call, what would be the best way to decode it into readable form within Solidity? "[INPUT]":"...
user avatar
  • 307
0 votes
1 answer
1k views

How do I encode a uint256 as bytes to feed to a function "sliceuint", with web3.js and solidity?

I have a function from a library: function sliceUint(bytes bs, uint start) internal pure returns (uint) { require(bs.length >= start + 32, "slicing out of range&...
user avatar
  • 122
2 votes
2 answers
2k 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 ...
user avatar
0 votes
1 answer
219 views

Is it possible to encode and decode two consequent byte arrays (with unknown sizes) using `abi.encode` and `abi.decode`?

Is it possible to encode and decode two consequent bytes using abi.encode and abi.decode (as following)? bytes memory data = abi.encode(data1, data2); // later on (bytes memory data1_recovered, bytes ...
user avatar
0 votes
1 answer
416 views

Member "concat" not found or not visible after argument-dependent lookup

I'm trying to concatenate a series of strings but I'm getting the following error: TypeError: Member "concat" not found or not visible after argument-dependent lookup in bytes storage ...
user avatar
  • 307
0 votes
0 answers
34 views

Solidity: Convert hex-string variable into bytes [duplicate]

I am looking for an option to efficiently convert a string passed to a function as a parameter, interpreted as hex, into bytes. In Solidity. I.e. doing the same that this piece of code would do bytes ...
user avatar
  • 150
0 votes
1 answer
2k views

Is it cheaper to store a string or convert it to bytes32 with abi.encodePacked()?

For example, in the code below, would it be cheaper to store the string in a mapping? Or to convert it to bytes32 and store it as bytes32 then convert it back to a string whenever I call it with ...
user avatar
  • 372
0 votes
1 answer
355 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 ...
user avatar
  • 1
1 vote
0 answers
267 views

How to measure Solidity variables size?

After reading different smart contracts from Uniswap teams and others, I understand that they stricly optimize the data to be stored on-chain, but how can they know what is the minimum size needed for ...
user avatar
0 votes
1 answer
183 views

Why would the type byte[] waste 31 bytes of space for each element?

The Solidity Docs (v0.8.4) states: The type byte[] 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 bytes type ...
user avatar
  • 158
4 votes
1 answer
394 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,...
user avatar
1 vote
1 answer
589 views

error comparing two bytes memory

I am testing a smart contract in Remix and I get the following error when trying to compare two bytes memory using "==": TypeError: Operator == not compatible with types bytes memory and ...
user avatar
1 vote
0 answers
12 views

Check string similarity percentage

How can I efficiently check the similarity of two strings? Or better yet, if I have an array of strings, I want to check that the given string is less than e.g. 80 % similar to any of the other ...
user avatar
0 votes
2 answers
1k views

How to assign a value to a bytes variable?

The function keccak256 () takes a bytes variable. You can't pass it bytes3 or byte []; it has to be of type bytes. How do you set that to a specific value? Here are some things I've tried, and the ...
user avatar
  • 190
0 votes
0 answers
183 views

Passing a hash value to function

I'm trying to authenticate users by comparing the hashes created by backend server and contract. Seems like "byte32" values cannot be coerced into strings. So, instead I'm converting it into ...
user avatar
0 votes
1 answer
397 views

String To bytes Error

I am trying to take user input strings and convert them to bytes32 and bytes5 for a contract built in Solidity 5.11, I tried 32 and 5 character long random strings respectively however they are not ...
user avatar
1 vote
1 answer
1k views

Why does retrieving stored data cost so much compared to using calldata in Solidity?

I have a function that I need to pass an arbitrary length array of addresses to. In remix, just calling a function that has an address[] costs around 30k gas for an array of 5 addresses: function fun(...
user avatar
1 vote
1 answer
112 views

In Solidity, Is there any situation, when string is necessary?

In Solidity, Is there any situation, when string is necessary? Please can u explain when I should use string instead of bytes or bytesN? Thanks!
user avatar
  • 21