Questions tagged [type-casting]

Type casting is the act of converting a value from one data type to another in a computer program.

Filter by
Sorted by
Tagged with
11
votes
2answers
4k views

What's the difference between 'address' and 'address payable'?

I saw a Solidity smart contract where some variables were declared with the type address and some were declared with the type address payable. What are the differences between the two? Do they store ...
3
votes
1answer
53 views

The most gas-efficient way to convert msg.data to int128?

Is there any more gas-efficient way to convert msg.data to int128? Here is some suggested impl: function bytesToBytes16(bytes b, uint offset) private pure returns (bytes16) { bytes16 out; for (...
1
vote
1answer
9 views

How to type cast fixed size byte arrays in solc 0.5.x?

In solc 0.4.28 I was able to do: uint foo = 42; bytes28 bar = bytes28(foo); but in 0.5.9 I'm getting the error: TypeError: Explicit type conversion not allowed from "uint256" to "bytes28". ...
1
vote
1answer
459 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 ...
1
vote
0answers
68 views

on the implicit type conversion rules in Solidity

Question in one sentence: Are there seperate implicit type conversion rules for function call resolution (other than the conversion rules for the remaining cases)? Regarding the array type ...
1
vote
1answer
30 views

Is there a need to check for null after casting?

Example: MyReceiver my = MyReceiver(_to); // To is an arbitrary address user provided my.someFunction(); // Do I need to check for my == null before calling? Strange enough I do ...
1
vote
2answers
181 views

How to type-cast a function pointer in Solidity?

I have a function that takes a parameter of type function (uint256) view returns (uint256). I want to pass it a pointer to a function of type function (uint256) pure returns (uint256). Obviously ...
0
votes
1answer
65 views

Solidity, how to cast SmartContract type to address

I am currently a bit stuck with my contract, the issue is following: I define a smartcontract type which is used for another address (aka address is smartcontract that provides the specified interface)...
0
votes
1answer
188 views

Cast contract to address payable

I am wondering if it is possible to cast a contract-reference to an address payable (in order to send to it money) or if it is possible to declare a contract-reference directly payable (with solidity >...
0
votes
2answers
440 views

abi.encodePacked with lot of zeros

According this documentation types shorter than 32 bytes are neither zero padded nor sign extended and When I use abi.encodePacked(address, uint, uint, address) I am getting a result with lot of ...
0
votes
1answer
473 views

A solidity 0.5.x function to convert adress-string to ethereum address

I'm looking for a Solidity 0.5.x compatible function to convert a string with and address like "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe" into a real address. I did use this function: function ...
0
votes
1answer
624 views

How to typecast keccak256 function with multiple arguments to uint256?

solidity ^0.4.19 (works) uint256 randNonce++; return uint256(keccak256(msg.sender, randNonce, now)) solidity ^0.4.24 (throws) uint256 randNonce++; return uint256(keccak256(abi.encodePacked(msg....
0
votes
1answer
435 views

Going from uint256 to uint8, is it possible?

I have a function that creates random numbers with a maximum value that you gives to it, the function was made to work with any uint256, and using % operator it returns a value between 0 and the ...