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
26
votes
6answers
8k 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/...
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 ...
1
vote
1answer
148 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
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
31 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
48 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!
1
vote
1answer
90 views

bytes seem to be limited to length 8

I'm pretty new to Solidity, and I was writing a contract to play around with dynamic arrays. My array length grows, but it seems to stop at length 8. Here's the contract I wrote: pragma solidity 0.4....
18
votes
7answers
9k views

How to convert a uint to bytes in Solidity?

Is there an easy way to convert a uint to bytes in 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
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
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[]...
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 ...
1
vote
1answer
571 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 ...
0
votes
0answers
39 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
58 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(...
0
votes
1answer
26 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
71 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
1answer
26 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 ...
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 = ...
1
vote
1answer
1k 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 ...
1
vote
1answer
58 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. ...
12
votes
6answers
8k views

How to convert an address to bytes in Solidity?

What is the recommended way to convert an address to bytes in Solidity?
2
votes
1answer
54 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
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
4
votes
1answer
255 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 ...
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 ....
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 ...
3
votes
1answer
406 views

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

Is it possible to slice variables in Solidity?
0
votes
1answer
234 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 =...
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 ...
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 ...
1
vote
1answer
49 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
39 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
76 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 ...
6
votes
3answers
3k views

How to convert bytes to uint in Solidity

Hey is there a way to convert bytes to uint in Solidity?
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
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
253 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
0answers
54 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 ...
2
votes
1answer
227 views

Reading bytes from bytes32 returns zero value (except first one)

Why always when I read some bytes from bytes32 only first read return a value, all following will return zeros? 0x00 bytes32 proof = 0x999e7640514e578f5b9b416469d270d559ad4186ef63da6cd63751373512735e;...
0
votes
1answer
57 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
290 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
295 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 ...
0
votes
1answer
240 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
69 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
393 views

Solidity: msg.data vs bytes

This is a simple contract that has a function input_data(bytes data) and a simple fallback function that passes msg.data to function input_data. contract test_input_data{ address owner; event ...
2
votes
2answers
37 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 ...
2
votes
1answer
238 views

Encode raw transaction data for inner transaction data for transfer using erc223

How do I encode data for inner transaction call of ERC223Receiver contract after transferring erc223 tokens to it? example: Contract ERC223Receiver { function foo(uint256 a) { // some ...
1
vote
1answer
685 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 ...
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 ...