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
1answer
17 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&...
0
votes
2answers
68 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 ...
0
votes
1answer
11 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 ...
0
votes
1answer
40 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 ...
0
votes
0answers
27 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 ...
0
votes
1answer
55 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 ...
0
votes
0answers
17 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 ...
0
votes
0answers
9 views

Can cheaper transactions be created by brute forcing RLP-encoded V,R,S values with leading zeros

When I make a raw txn using RLP-encoding I note that the VRS values are not deterministic. Sometimes the R and S values can have leading zeros, these get omitted, thus making the transaction data ...
1
vote
0answers
33 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 ...
0
votes
1answer
19 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 ...
4
votes
1answer
113 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,...
0
votes
1answer
37 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 ...
1
vote
0answers
6 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 ...
0
votes
0answers
19 views

How much is the biggest BytesX type in solidity? (Bytes32?)

I'm trying to keep it simple while returning and array of strings. I want to return a fixed bytesX array with small text. How much is the possible X in the BytesX type? In Remix it seems to be Bytes32....
0
votes
2answers
392 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 ...
0
votes
0answers
28 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 ...
0
votes
1answer
65 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 ...
1
vote
1answer
407 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(...
1
vote
1answer
80 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!
0
votes
1answer
83 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'; ...
0
votes
0answers
171 views

Convert address to bytes8

I want to convert address to bytes8, but I only managed to convert address to bytes this way: bytes memory shortAddress = new bytes(8); for(uint i = 0; i < 8; i++) { shortAddress[i] = byte(uint8(...
0
votes
1answer
131 views

How do you call a fallback function that uses msg.data for parameters

I am trying to complete a homework assignment, so I am not doing something malicious. The goal is to learn the vulnerabilities of contracts. I want to call the fallback function of wallet to trigger ...
0
votes
1answer
51 views

Write uints as bytes to create one larger bytes

I would like to take three uints and essentially concatenate them together as one bytes variable. What's the simplest way I can do that? I've seen many posts about people writing full-blown code to ...
1
vote
1answer
102 views

Error: Invalid bytes6 value

I'm trying to create a TicketPurchaser application. Here is the whole code for the smart contract, although I think the problem I'm asking about is a type error, most of the code is not important. ...
2
votes
1answer
286 views

Decoding call inputs

Using a smart contract's ABI and the input data from a transaction on Etherscan, I am able to decode the input data using this tool: https://lab.miguelmota.com/ethereum-input-data-decoder/example/ ...
0
votes
0answers
41 views

How to check if key exists in array of bytes? [duplicate]

I have an array of bytes10 inside a mapping, like this: mapping(uint => bytes10[]) public Players; I want to check if a certain index exists in the bytes10 array. I tried to check it with ....
0
votes
1answer
36 views

Why does declaring returns type bytes memory give a compiler error?

If we have the following code: pragma solidity >= 0.5; contract stringsContract { function takesTwo(string memory str, uint idx) public pure returns (bytes memory) { bytes memory ...
4
votes
1answer
2k views

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

Is it possible to slice variables in Solidity?
1
vote
1answer
392 views

Bytes memory as a function parameter increase gas cost

I've a smartcontract compiled with pragma solidity ^0.4.0; version and one function receives a bytes parameters which contain a long string. In these version it doesnt need to set the parameters as "...
2
votes
0answers
71 views

Performing a C-like pointer increment in Solidity

I have a memory or calldata struct with an attribute of type byte[]. I want to have a memory variable with that data, except for the first n Bytes. In C, you'd just write typename variablename = ...
0
votes
1answer
31 views

Can I call a contract function in the “data” element via web3, especially when I am using conversion to bytes?

In other words, say In Python I have something like that: signed_txn = w3.eth.account.signTransaction(dict( nonce=w3.eth.getTransactionCount('0x0000000000000000000000000000000'), gasPrice = ...
0
votes
1answer
26 views

Is it possible to store an array of bytes (so something like bytes[])?

Edit The original question below seems to work as expected. The issue happens with the call return data is empty. When the response is nothing, the code below fails. The following works, but is ...
0
votes
1answer
293 views

Proper way to save a public key as bytes in a contract

I need to save public keys in many contracts. Despite that this choice costs, I store them as bytes (I know that the best way is to split the key and store it to different bytes32 constants, but that ...
0
votes
1answer
1k views

web3 - web3.utils.toHex zeros length

I'm trying to send 12 integer as bytes to a contract function. If I use web3.utils.toHex(12) the variable will become 0xc The problem is that when contract function parses bytes, it reads uint256, ...
0
votes
1answer
40 views

Combine BytesX and BytesY to create BytesZ?

I am working on a Solidity Smart Contract (^0.5.10) where I want to return an array of Ratings elements. struct Ratings { address restaurant uint8 score }; Since returning Rating[], string[]...
1
vote
2answers
204 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 ...
0
votes
1answer
81 views

Remix - Pass bytesX to function

Using Remix, I can't pass a set of bytes ["0xee", "0xff", "0xgg", "0xhh", "0xii"] to my function function foo(bytes5 input) { // Do something } I've already tried all these formats: ["0xee", "...
1
vote
1answer
742 views

Solidity: Convert from “bytes memory” to “address”

I'm using solidity 0.5.0 and when i try to compile my contract it's giving me this error Explicit type conversion not allowed from "bytes memory" to "address" my code is this: /// @dev get broker ...
0
votes
1answer
1k views

Solidity, problem with bytes decoding

back with another question towards solidity Guru's. I am trying to call a method which takes bytes array as an input, but either I am encoding this parameter wrongly or decoding it wrongly. Solidity ...
5
votes
2answers
3k 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); } //...
2
votes
1answer
391 views

How to set byte in fixed-size byte array?

function computePairId(bytes20 base, bytes20 quote) public view returns (bytes4) { bytes32 pairHash = sha256(abi.encodePacked(base ^ quote)); bytes4 pairId; pairId[0] = pairHash[28]; ...
1
vote
1answer
138 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). ...
0
votes
1answer
106 views

How to get the last n bytes of a bytes32 in assembly?

I have the following code: function getBytes5() public returns (bytes5) { uint256 x = now; bytes memory inBytes = new bytes(32); bytes5 outBytes5; assembly { mstore(...
1
vote
2answers
2k 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 ...
2
votes
2answers
60 views

Validate bytes16 argument size

I have a function which accepts one bytes16 argument. function start( bytes16 reference ) I have some events which I use to track behaviour, using reference as the index. Everything works ...
0
votes
1answer
120 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 ...
2
votes
1answer
1k 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 ...
3
votes
2answers
3k 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 ...
5
votes
3answers
2k 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 ...
2
votes
0answers
1k 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: // ...