Questions tagged [bytes]
both the fixed-size array byte, bytes1...bytes32, and the dynamic array bytes types in Solidity
194
questions
1
vote
1
answer
1k
views
Possible to do bitwise operator on byte array (bytes)?
Or do only bitfields like bytes32 support the xor function (a ^ b)?
Related, Why can't we do bitwise operations on Bools?
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"...
1
vote
1
answer
2k
views
Storing IPFS hash in solidity
I'm storing IPFS hash in solidity using this guide
https://www.reddit.com/r/ethdev/comments/6lbmhy/a_practical_guide_to_cheap_ipfs_hash_storage_in/
const cid = "...
2
votes
2
answers
3k
views
Convert 'bytes memory' to 'bytes8'
In solidity, how can I convert bytes memory buffer to bytes8 type?
Example:
bytes8 buf2 = bytes8(memory_buf); // TypeError: Explicit type conversion not allowed
1
vote
0
answers
241
views
Convert an hexadecimal string to bytes in pure Solidity
I am using Oraclize to recover a signature from a website. When I have it, I have to check if the address who signed it is correct. To do that, I would use ECRecovery from Zeppelin.
The signature ...
3
votes
1
answer
740
views
Any way to convert the characters that make up bytes into a string?
For example if I have a bytes4 variable that is "0xabcd1122" is there any way to turn this into a string with a value of "abcd1122"? I am asking because I want to assign a unique identifier to a token ...
2
votes
1
answer
426
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 ...
2
votes
1
answer
699
views
What is the best type to store a signature on chain?
I'm signing a hash and getting a string with 130 bytes. What is the best way to store this signature later in my smart contract?
Should I just go ahead and split it into r,s, and v and store it in ...
1
vote
1
answer
2k
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 ...
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 ...
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
1k
views
Uint to Bytes32 conversion -- how does this make sense [duplicate]
Pretty simple code here:
pragma solidity ^0.4.13;
contract Oracle{
event Print(string _name, bytes32 _value);
bytes32 key;
function Store(bytes32 _key) returns (bytes32) {
key = ...
0
votes
1
answer
685
views
Storing sha3 hash in a useful way
I'm trying to store the sha3 hash of a string in a struct.
The problem is that its size exceeds what fits into a bytes32 type. As such, the alternative would be to have it in a bytes[] or string[]. ...
2
votes
1
answer
436
views
How can I split a 56 character input and store them in two Bytes32?
I want a function to accept, store and output 56 characters. My other question (How to return bytes array in Solidity Contract) shows that I tried to return bytes[], but it gives me an error:
...
6
votes
1
answer
2k
views
How to store and load an IPFS hash as bytes32 in contract written in Solidity [duplicate]
An IPFS hash can be expressed in hexadecimal which is 34 bytes long.
Afaik it is most efficient to store fixed length values in a Ethereum contract, thus a maximum of 32 bytes (bytes32).
Because the ...
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 ...
2
votes
1
answer
109
views
Converting 8x8 board game to bytes
I'm trying to save memory by storing a game's 8x8 board as a binary value but am having trouble:
Each square on the board can be either 10 (black), 11 (white), or 00 (empty) and there are 64 squares. ...
2
votes
1
answer
413
views
How do I combine integers, to save gas on transaction data and storage?
For example, I have two integers: one is uint128, another is uint64. I want to store those two integers by converting them to bytes24. And when I use them, parse the bytes24 to uint128 and uint64.
...
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....
20
votes
2
answers
3k
views
The Unicorn Mystery - Solidity fails to decode valid UTF-8 encoded bytes
The Ethereum Foundation has the Unicorn token to encourage donations, and the unicorn emoji is represented by three bytes. How in the world did they do that?
I know that Solidity supports unicode ...
3
votes
0
answers
258
views
Checking if an address is encoded on the end of a bytes array
I have a bytes array. Within the last 20 bytes, there is an address stored.
I am trying to compare the address at the end of this array to the address of a msg.sender, and am wondering if there is 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/...
4
votes
1
answer
2k
views
How does keccak256 concatenate values inside a Solidity smart contract?
I'm developing a smart contract in Solidity in which I need to evaluate the hash of two concatenated bytes32. I don't have to concatenate the bytes32 since the sha3 function supports more than one ...
3
votes
3
answers
5k
views
Ethereum: invoking contract function with bytes32 array
i have the following contract function
function voteForAnswer(bytes32 questionKey, bytes32[] answerKeys)
returns (bool success)
{
// is the sender allowed to do this?
if (voters[msg....
28
votes
2
answers
28k
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 ...
2
votes
1
answer
888
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;...
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 ...
2
votes
1
answer
817
views
What is the best way to write into a byte variable at a predefined position?
According to the Solidity documentation one can access the byte at position k in for example a Bytes16 variable source with the following code:
pragma solidity ^0.4.8;
contract example {
Bytes16 ...
3
votes
1
answer
3k
views
Convert bytes32 to ordered bytes2, bytes4, etc
For example,
I need to slice my bytes32 variable to chunks, and put result into 5 variables, saving the order, bytes2, bytes2, bytes2, bytes2, bytes8, bytes1 (aka byte), the order should be saved and ...
4
votes
1
answer
6k
views
What does the bytes format “\xFF\xFF\xFF” mean and how it is interpreted?
bytes sig = hex"aca7da" and bytes sig = "\xac\xa7\xda" seem to be the same thing, ASCII interpreted as hex. Does the format "\xFF\xFF\xFF" somehow code that it should be read as hex?
2
votes
2
answers
974
views
convert from bytes32 to bytes with format "\xFF\xFF\xFF"
I'm using the ECVerify library and in the test examples in that contract, function ecverify(bytes32 hash, bytes sig, address signer) returns (bool) {} expects bytes sig in the format "\xFF\xFF\xFF...",...
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?
5
votes
1
answer
934
views
Storing hardcoded calldata in a Solidity contract
I've been trying to store a hardcoded call in a contract, but I'm getting confused on the encoding.
Let's say I have the following 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 ...
11
votes
1
answer
5k
views
Error when using string type as a mapping key
The following code doesn't compile, because compiler needs a mapping key to be of fixed-size type in order to create accessor for a public mapping. I'm using here string type. My strings are 24 byte ...
3
votes
1
answer
906
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 ...
21
votes
2
answers
19k
views
In solidity, how can you set a bytes variable to empty
I tried this
contract MyContract{
bytes public data = 0x3333;
function clearData(){
data = 0x;
}
}
But solc doesn't like me trying to set data to 0x
5
votes
1
answer
2k
views
How do you get a string value from msg.data in the fallback function?
Im trying to do what The DAO did and do something when a sender sent money to my contract address. But im not sure how to.
The something I want to do is to just create a new user in a mapping with the ...
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?
3
votes
2
answers
2k
views
Can you extract bytesn from bytes in Solidity?
How would you create a Solidity function with the following signature?
function extract(bytes data) returns (bytes20 subdata)
Where subdata is a contiguous slice of data and data.length > 20.
...
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 ...
6
votes
1
answer
2k
views
In solidity, how can I hardcode bytes > 32 bytes
I want to create a contract with a hardcoded bytes variable.
contract Test(){
function Test(){
bytes x = 0x..... //greater than 32 bytes
}
}
The above returns a Type int_const not ...
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 ...
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?