Questions tagged [bytes]
both the fixed-size array byte, bytes1...bytes32, and the dynamic array bytes types in Solidity
194
questions
8
votes
2
answers
9k
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
1
answer
1k
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];
...
3
votes
1
answer
821
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
1
answer
496
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(...
7
votes
2
answers
7k
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
2
answers
215
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
1
answer
319
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
...
3
votes
1
answer
3k
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 ...
6
votes
1
answer
8k
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
3
answers
3k
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 ...
4
votes
0
answers
2k
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
1
answer
288
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
1
answer
196
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
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 ...
4
votes
1
answer
597
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
0
answers
109
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 ...
3
votes
1
answer
4k
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 ...
7
votes
1
answer
642
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
1
answer
300
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
1
answer
1k
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
1
answer
94
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
1
answer
117
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);
...
2
votes
1
answer
2k
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
1
answer
784
views
Make character count, count emoji's as 2
I have the following function:
function getCharacterCount(string str) constant
returns (uint length)
{
uint i=0;
bytes memory string_rep = bytes(str);
while (i<string_rep.length)
{...
0
votes
1
answer
747
views
How to save a number as bytes16 in a smart contract and retrieve the number in javascript?
I have the following smart contract to clarify the issue.
pragma solidity ^0.4.17;
contract Test {
bytes16 number;
function getNumber() public view returns(bytes16) {
return number;
...
2
votes
1
answer
1k
views
Optional Multi-Parameter Call to External Contract
I would like to half implement ERC223 for business reasons. This means being able to notify other contracts when they receive tokens, but not revert if the recipient is a contract that doesn't ...
1
vote
1
answer
266
views
Pushing bytes to EVM stack
I am sorry if this question seems to be somewhat bad, but I am trying to work out the stack representation of the following opcodes:
PUSH1 60
PUSH2 40
would the stack representation of this be:
[...
1
vote
2
answers
773
views
How to declare a fix bytes type more than bytes32?
I am facing "UnimplementedFeatureError: Nested dynamic arrays not implemented here.
" when I try to emit an event contains bytes [] data;
I have a fix size of data which is 65bytes but there is no ...
2
votes
1
answer
2k
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 ...
3
votes
2
answers
2k
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
1
answer
631
views
How to concat different parameters type in one parameter (bytes) and extract them?
I have a function that take 3 parameters
function askForHelp (string id, uint256 times, bytes32 signature) {
//do something
}
I want to change it to:
function askForHelp(bytes data) {
string ...
2
votes
1
answer
309
views
How much data can be stored in a bytes type in a smart contract?
What are some estimates of how much data can be stored in a bytes type in a smart contract?
contract test {
bytes data;
function storeData(bytes _data) {
data = _data;
}
}
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?
0
votes
2
answers
87
views
What is the purpose of the variable "maxlength" in this function?
I am trying to understand the function "appendUintToString" from Solidity concatenate uint into a string? The function is recreated below. For now, I am trying to understand what the integer "...
0
votes
0
answers
509
views
Why are elements in a byte array padded to 32 bytes?
On readthedocs.io, it says that byte[] or bytes1[] elements are padded to 32 bytes. I thought at first it was for even distribution, but distribution would still be even if each element had exactly ...
2
votes
1
answer
1k
views
How does solidity find positions within a bytes array?
My understanding of arrays is that the initial storage point of the array is used to find the beginning of the array, and finding specific items in the array from that point forward require even ...
0
votes
1
answer
679
views
Compare byte32 Keccak256 to the same Keccak256 stored in a byte32 array
I want my function to be able to accept an array of Hashed secrets as a parameter and since solidity doesn't allow for strings, I had to use a byte32[].
The string parameter is an intake for the user'...
3
votes
0
answers
765
views
When is it appropriate to use "bytes" (or bytes1 - bytes32) in Solidity?
Upon reviewing prior questions and answers, I understand that "bytes" should be used for raw byte data of arbitrary length and "string" for UTF-8 data of arbitrary length. I also understand that if I ...
0
votes
1
answer
2k
views
Understanding Bytes16 Representation in Solidity
I have a simple function printing byte a byte the value of a bytes16 0x20010db8000000000000000000000001 and I get the following result.
['0x20', '0x01', '0x0d', '0xb7', '0xff', '0xff', '0xff', '0x65' ...
0
votes
1
answer
441
views
Im using Oraclize to get a random number but the N of bytes confuses me
It seems like when I give the Oraclize query a N of 4, it always returns a 19 digit number, this might be a coincidence but I think not. What if I actually want a number between lets say 0 and 10 * 10^...
0
votes
1
answer
522
views
Check bytes32 exists in bytes32[]
I would like to know if this is possible to check in a simple way if a bytes32 value exists in an array of bytes32 value please?
contract Test {
struct TestStruct {
string name;
...
0
votes
0
answers
239
views
Dynamically-sized byte array is value type?
In the doc, it says
bytes:
Dynamically-sized byte array, see Arrays. Not a value-type!
string:
Dynamically-sized UTF-8-encoded string, see Arrays. Not a value-type!
Not a value-type!
If ...
0
votes
2
answers
2k
views
How to store hash of an Excel file in Blockchain? [closed]
I'm sorry if this question does not seem to be logic.
I want to store hash of an Excel file in Blockchain by following function :
//byte fileHash;
event LogsetFileHash(byte _hashValue);
...
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) }
}
...
2
votes
0
answers
839
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;
...
0
votes
2
answers
1k
views
Store literal bytes4 as string
Does anyone have the 'somefunc' below to store bytes (specifically bytes4) as a string? Ideally something to get method_s in the code below:
contract Test{
bytes4 public method;
string public ...
1
vote
1
answer
151
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....
4
votes
1
answer
368
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 ...
1
vote
1
answer
648
views
Help storing byte array
pragma solidity ^0.4.0;
contract Storage {
uint8[5][5] public data;
function Storage() public {
}
function setData(uint8[] _data) {
for (int i=0;i<5; i++)
...
5
votes
2
answers
6k
views
How do I check in solidity if interface is supported?
In the new ERC721 documents (erc721.org), the interface says that one of the functions should be:
/**
* @dev Returns `true` if the contract implements `interfaceID` and`interfaceID` ...