Skip to main content
Share Your Experience: Take the 2024 Developer Survey
73 votes

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

Data storage and representation in the EVM The address and address payable types both store a 160-bit Ethereum address. The concept of payable and non-payable addresses only exists in the Solidity ...
Jesbus's user avatar
  • 10.5k
18 votes
Accepted

How to convert a uint256 type integer into a bytes32?

Just use bytes32(u). It's as easy as that because they are both 2^256 (unlike bytes). No need for a separate function, but to clarify: function convert(uint256 n) returns (bytes32) { return ...
eth's user avatar
  • 85.9k
14 votes

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

Yes, they store the same data, that is a valid ethereum address. The difference is that the compiler (at compile time) when encounter an “address payable” is ready to allow (if required in the ...
Rick Park's user avatar
  • 3,194
10 votes
Accepted

Convert/cast an address to uint256

Solution: uint256(uint160(xAddr))
dNyrM's user avatar
  • 918
6 votes
Accepted

Solidity, how to cast SmartContract type to address

Just cast to address: if (address(sampleInterface) != address(0)) {
user19510's user avatar
  • 28.1k
5 votes

Going from uint256 to uint8, is it possible?

You can just cast it: uint8(number)
user19510's user avatar
  • 28.1k
4 votes
Accepted

How to typecast keccak256 function with multiple arguments to uint256?

Is it possible your function returns(uint8) ? This compiles: pragma solidity 0.4.24; contract HashToUint { uint256 randNonce; function test() public returns(uint) { randNonce++; ...
Rob Hitchens's user avatar
  • 55.3k
4 votes
Accepted

Difference between a byte in a bytes32 and a byte in a bytes structure

The basic unit of work of the EVM, usually called a word, is 32 bytes. This means that for the EVM, no matter whether we want to express 1 or 2^150, it will take 32 bytes, or 256 bits, to do so. ...
Daniel Perez's user avatar
4 votes
Accepted

Explicit type conversion not allowed from "uint256" to "int32"

As per the 0.8.0 changelog: There are new restrictions on explicit type conversions. The conversion is only allowed when there is at most one change in sign, width or type-category (int, address, ...
hroussille's user avatar
  • 7,721
3 votes
Accepted

What type is "MyInterface.MyCustomError" in Solidity?

I eventually figured out half of the answer on my own by trying to force cast the type to bytes4. It seems like MyInterface.MyCustomError is of type function() pure: Explicit type conversion not ...
Paul Razvan Berg's user avatar
3 votes

What are the gas costs for type casting or conversions?

Short version, it depends, but you can expect between 42 and 46 gas, when tested with Solidity 0.8.7+commit.e28d00a7. General thoughts - casting to int/uint is cheapest, it costs ~2 gas to cast ...
FrozenKiwi's user avatar
3 votes
Accepted

How to convert uint8 to uint256 bitmap in solidity?

You would use the shift operator. In your example: uint8 value = 9; uint256 mask = 1 << value; See https://docs.soliditylang.org/en/v0.8.10/types.html#integers
Richard's user avatar
  • 4,994
3 votes
Accepted

Cast uint to int

The answer is: int64 i_number = int64(uint64(u_number))
João Abrantes's user avatar
3 votes

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

I figured it out... You have to cast the uint to the same length so in this case it would be: bytes28 bar = bytes28(uint224(foo)); If it were bytes16 it would be: bytes16 bar = bytes28(uint128(foo))...
okwme's user avatar
  • 293
3 votes
Accepted

How to convert bytes32 to uint8 in solidity ^0.5.0?

I don't think it ever was because bytes is a dynamic array of byte which means it has a length. Is it possible you saw bytes32 conversion to unit? That is totally acceptable in that both are 32-byte ...
Rob Hitchens's user avatar
  • 55.3k
3 votes
Accepted

Grab first 20 bytes of 32-bytes input

You could cast the value in byte20 case i.e(bytes20(bytes32)).
Saediek's user avatar
  • 46
2 votes

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

I was confused about the payable address but after a lot of searching, I find the best use case of a payable address. when you are working with a solidity smart contract then you use the payable ...
Mr.Error's user avatar
2 votes

abi.encodePacked with lot of zeros

uint is 256 bit, so everything is encoded correctly: first parameter: 14723a09acff6d2a60dcdf7aa4aff308fddc160c second parameter: 000000000000000000000000000000000000000000000000000000000000007b ...
Nulik's user avatar
  • 4,061
2 votes
Accepted

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

This should work function foo(bytes m) public pure returns (uint128) { bytes32 b; assembly { b := mload(add(m, 0x10)) } return uint128(b); } The idea is to load the 16 bytes ...
Ismael's user avatar
  • 30.5k
2 votes

How to type-cast a function pointer in Solidity?

Update: In recent Solidity versions, pure function pointers are now implicitly convertible to view function pointers. I did not find a solution to this problem, so I submitted an issue on Github: ...
Jesbus's user avatar
  • 10.5k
2 votes
Accepted

ParserError: Expected primary expression - address payable - payable()

The reason you got that error because there is no function name payable that take an integer value and return payable address. The parser doesn't recognise it and give you an error like that. I don't ...
haxerl's user avatar
  • 1,124
2 votes
Accepted

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

From oraclize contract function parseAddr(string memory _a) internal pure returns (address _parsedAddress) { bytes memory tmp = bytes(_a); uint160 iaddr = 0; uint160 b1; uint160 b2; ...
Ivan Zakharov's user avatar
2 votes
Accepted

Cast contract to address payable

This works. pragma solidity 0.5.1; contract A { function() external payable { // fallback logic goes here ... } } contract B { constructor() payable public { A a = ...
Rob Hitchens's user avatar
  • 55.3k
2 votes
Accepted

uint16 conversion issue

This line: require(number_ < 1 && number_ >= 5, "This number must be between 1 ~ 5"); This is an impossibility. It should say: require(number_ >= 1 && number_ <= ...
Rob Hitchens's user avatar
  • 55.3k
1 vote
Accepted

Safe convertion/casting from uint256 to int256

require(msg.value <= 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) // or type(uint256).max/2 would work. Its unlikely someone try to send more than 5.7*10^58 ETH to your ...
Foxxxey's user avatar
  • 4,337
1 vote
Accepted

How can I call a function which needs string calldata as input parameter?

Functions that accept calldata must be called externally. Calldata is read-only and generated through a blockchain call. A contract can call itself externally by calling as this.f("abdce"); ...
Yetik's user avatar
  • 339
1 vote

How can I call a function which needs string calldata as input parameter?

pragma solidity 0.8.7; contract Test { constructor(uint160 _magic) { this.f("abcde"); } function f(string calldata s) external { // emit FF(s); } } This works. ...
Yongjian P.'s user avatar
  • 4,208
1 vote

Get first digit of number

Look at this solution. I show how to get any digit at any index of a number, and how to get the first digit of a number (from left to right). How to get the last digit of a number. Read the comments ...
Jeremy Then's user avatar
  • 4,609
1 vote
Accepted

Reading return data from a Solidity `call`

Returndata will be abi encoded bytes. You must decode these first providing the correct types. Example: (uint256 a, address b) = abi.decode(returnData, (uint256, address));
phaze's user avatar
  • 1,595
1 vote

uint16 conversion issue

The first paramter in require must be true for it to continue. Currently, your code checks for a number that is smaller than 1, and larger than or equal to 5. If you change the require call to the ...
Morten's user avatar
  • 6,017

Only top scored, non community-wiki answers of a minimum length are eligible