Questions tagged [bytes]
both the fixed-size array byte, bytes1...bytes32, and the dynamic array bytes types in Solidity
26
questions
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"...
28
votes
2
answers
27k
views
How to pass arbitrary bytes to a function in Remix (2017)
According to this question I could write a simple contract like:
contract SimpleStorage {
bytes input;
function setInput(bytes enterBytes){
input = enterBytes;
}
}
Using "0x1234" as a ...
34
votes
7
answers
16k
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/...
21
votes
6
answers
25k
views
How to convert an address to bytes in Solidity?
What is the recommended way to convert an address to bytes in Solidity?
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 ...
9
votes
2
answers
8k
views
How to concatenate a bytes32[] array to a string?
I am passing an array of bytes32 to my function. This array can contain numbers or hashes etc. to identify an object. In a new use case I want to pass a URL to this function by splitting the URL into ...
36
votes
7
answers
30k
views
How to convert a uint to bytes in Solidity?
Is there an easy way to convert a uint to bytes in Solidity?
40
votes
2
answers
31k
views
What is the difference between bytes and bytes32?
I know bytes32 stores 32 bytes, but what does bytes store? And what is the difference between the two?
13
votes
4
answers
13k
views
How to convert bytes to uint in Solidity
Hey is there a way to convert bytes to uint in Solidity?
12
votes
1
answer
15k
views
How can I slice bytes, strings, and arrays in Solidity?
Is it possible to slice variables in Solidity?
5
votes
2
answers
7k
views
Concat two bytes arrays with Assembly
Does anyone know how to efficiently concat two bytes arrays with assembly language to save gas costs? Currently I have
function mergeBytes(bytes param1, bytes param2) returns (bytes) {
bytes ...
17
votes
2
answers
8k
views
Convert struct to bytes in Solidity
Is there any convenient way to convert (e.g. cast) a struct type to bytes?
I'm trying to call a method like the one below (taken from here) where you can pass some (arbitrary) contextual info that ...
8
votes
1
answer
7k
views
How to pass arbitrary bytes to a function (2016)
I'm trying to implement a contract which will store bytes blob, a dynamic bytes array.
For example I have following struct:
Sign {
address signer;
bytes16 signType;
bytes sign;
}
plus ...
7
votes
1
answer
6k
views
Solidity assembly question - mstore
I was searching on how to convert a uint to bytes in Solidity and found this answer.
function toBytes(uint256 x) returns (bytes b) {
b = new bytes(32);
assembly { mstore(add(b, 32), x) }
}
...
7
votes
1
answer
639
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 ...
5
votes
2
answers
9k
views
passing String object with "0x.." value as bytes32 to a solidity function
I'm having trouble to pass bytes32 to a solidity function from javascript. It's converting to an incorrect bytes32 in solidity.
The javascript is a test, running with truffle v3.3.1 on testRPC v3.0.5....
5
votes
2
answers
7k
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 ...
5
votes
1
answer
7k
views
How to return bytes array in Solidity Contract
I'd like to input & output an array of bytes (of approximately 56 characters each). The input works but the compiler gives a TypeError on bytes[] for the output function:
TypeError: Internal type ...
3
votes
1
answer
904
views
Why can't we do bitwise operations on Bools?
So I'm running an if/then comparator function with bools. It's essentially and xor function but when I tried a ^ b i get operator not compatible with types bool and bool.
Given that bools (in ...
2
votes
1
answer
884
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;...
2
votes
0
answers
835
views
How to input byte array to a function?
I have a function in my contract and it have bytes1[4] array parameter and returns.
function foo(bytes1[4] memory _mdata) public pure returns(bytes1[4]){
bytes1[4] memory result;
uint8 i;
...
2
votes
1
answer
2k
views
Problem using created array from assembly
I've used the code from the solidity assembly tutorial here to create a single dimensional array in assembly. The code is as follows:
contract C {
function f(uint a, uint b) constant returns (uint[]) ...
1
vote
1
answer
115
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 ...
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
979
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 ...
0
votes
0
answers
41
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 ...