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
0answers
18 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
0answers
19 views

file to byte array problem [closed]

Converting a file to a byte array to be stored as bytes[] in solidity I am able to convert the file using the following function, but when I send it to my smart contract I get error "TypeError: ...
0
votes
1answer
20 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
28 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
46 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
20 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
36 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
24 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
23 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
54 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. ...
0
votes
0answers
22 views

How to convert an address in string with correct case (upper or lower) of each symbol in Solidity? [duplicate]

Is there any recommended way to convert an address into string? Also it will be great if there is possible to safe the characters cases
2
votes
1answer
52 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
33 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
27 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 ...
3
votes
1answer
342 views

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

Is it possible to slice variables in Solidity?
1
vote
1answer
43 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
37 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
26 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
23 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
72 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
0answers
21 views

What is an efficient way for converting 'bytes4' into 'bytes' or 'byte[4]'?

I am currently converting bytes4 into bytes like this: function convert(bytes4 x) public { bytes memory y = abi.encodeWithSelector(x); ... } Is there a more efficient way? Also, is there an ...
0
votes
1answer
231 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
33 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[]...
0
votes
0answers
50 views

web3.utils.toHex but combined, hex concat?

I'm currently working with bytes in Remix. The goal I wanted to achieve was to put variables into bytes in one function, copy bytes, paste bytes to another function as a parameter and unpack these ...
1
vote
2answers
82 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
56 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
285 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
278 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
1k 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); } //...
0
votes
1answer
232 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
64 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
55 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
1k 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
35 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
68 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 ...
1
vote
1answer
674 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
1k 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 ...
4
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 ...
1
vote
0answers
606 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: // ...
1
vote
1answer
147 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 ...
1
vote
1answer
99 views

Bit manipulation (python vs solidity)

I am working on translating code from Python to Solidity and everything has worked up until this last loop. Cannot figure out where the difference is in the code (besides the bytes being in decimal ...
0
votes
1answer
244 views

Is there any way to return multiple structs (or the fields from them) in Solidity when one of the struct fields is of type `bytes`?

I would like to return structs keys from my contract to display on the frontend, ideally 10 at a time. I understand that if I make keyByOwner public I can return a single key at a time with the getter ...
3
votes
1answer
174 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) { ...
0
votes
0answers
50 views

How strings are stored in Solidity on a low level?

So, the question is in the title, let me describe scenario: I make raw rpc call via method eth_call to contract Token to a function name to get token name of type string. Below is my original ...
1
vote
1answer
1k 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 ...
6
votes
1answer
159 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 ...
1
vote
1answer
142 views

Cant pass a byte into a function without an error

The code below compiles perfectly and seems to work, however when I try to call addTask like: "item1", 1, "i1", 1 I have tried every possible variation of quotes parenthesis, brackets... ...
1
vote
1answer
385 views

Assembly opcodes

I need some help understanding assembly functions pragma solidity ^0.4.23; contract Test { function addTest() public pure returns (uint c) { bytes memory b = new bytes(1024); assembly { c:...
3
votes
1answer
57 views

Representing biometric data with solidity variables — bytes 32 perhaps? [closed]

would like to know which variable type comes closest to representing biometric binary data? Assuming computing storage and power grows exponentially and network latency can be further reduced, would ...
2
votes
1answer
59 views

How to extract int128 from msg.data as first argument?

Let's say we have Ethereum smart contract with fallback function: event onDeposit(int128 indexed key, address from, uint amount); ... function () payable public { require(msg.value > 0); ...