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
0 votes
0 answers
8 views

Reading return data from a Solidity `call`

Given a call to an external contract, (bool success, bytes memory returnData) = addr.call(abiEncoding); how do I read a single value of a given type from the return data, without using assembly? The ...
user avatar
0 votes
1 answer
236 views

Convert/cast an address to uint256

Following these details on the values that make part of the calldata that goes into an L2 message in Arbitrum: https://developer.offchainlabs.com/docs/arbos_formats#l2-messages How do you convert/...
user avatar
  • 307
2 votes
2 answers
46 views

What type is "MyInterface.MyCustomError" in Solidity?

I'm toying with the recently introduced custom errors. Here's a mini replica of my set up: interface MyInterface { error MyCustomError(); } I'm writing my tests in Solidity and I'd like to use ...
user avatar
0 votes
1 answer
219 views

What are the gas costs for type casting or conversions?

I frequently cast unsigned integer types (e.g. uint8) to signed integer types (e.g. int24), this requires 2 casts first to the unsigned type (e.g. uint24) then to the signed type (int24). I know I ...
user avatar
  • 230
0 votes
1 answer
187 views

How to convert uint8 to uint256 bitmap in solidity?

Say I have a uint8 with binary value 0000_1001 i.e. 8+1=9 in decimal. How do I convert this uint8 in a gas efficient way to a uint256 which represents a bitmap?, such that in this example the 9th bit(...
user avatar
  • 230
0 votes
1 answer
385 views

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

I am having following error with the latest solc (0.8.10) version, which was not happening in the solc versiona 0.7.* CompilerError: solc returned the following errors: TypeError: Explicit type ...
user avatar
  • 7,689
1 vote
1 answer
229 views

Cast uint to int

My function receives a bytes string. I convert one byte to an uint8. uint8 u_number = uint8(data[0]); I now want to pass this number to a function that only accepts int64, so I need to cast it first. ...
user avatar
1 vote
1 answer
323 views

Safe convertion/casting from uint256 to int256

I have a small function getConversionRate() that converts a value in ETH to USD using AggregatorV3Interface.latestRoundData(). Here priceFeed is a implementation of AggregatorV3Interface using ...
user avatar
  • 113
1 vote
2 answers
178 views

uint16 conversion issue

So I want to test this Solidity code using Typescript but faced with a type conversion issue. uint16 base; function addNumberToBase(uint16 number_) { // This number must be between 1 ~ 5 ...
user avatar
  • 73
0 votes
1 answer
165 views

How to type-cast contract type to address type?

Contracts can be explicitly converted to and from the address type. In solidity docs, it's mentioned that the contracts can be converted into an address type. But when I try to do so the compiler ...
user avatar
0 votes
0 answers
175 views

type conversion from fixed and ufixed to string

i have a contract and within the contract i have a function which takes two variables from the input from the user, one of them is integer and one of them is double (both are necessary to the ...
user avatar
  • 41
0 votes
1 answer
620 views

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

One can read in the docs: A bytes is similar to byte[], but it is packed tightly in calldata and memory. What does this exactly mean? For instance, according to this link, why should we convert a ...
user avatar
2 votes
2 answers
4k views

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

I'm trying to get myself familiarized with ethereum. So I was going through a repo, and tried to rum the code : function transferFundOnResolve(uint cid) private { // Could also be used: ...
user avatar
1 vote
2 answers
372 views

How could I convert uint type to bytes type in solidity?

I want to convert uint type data to bytes type in solidity. I've found a answer about this which is like below. function toBytes(uint256 x) public pure returns (bytes memory b) { b = new bytes(32)...
user avatar
  • 181
1 vote
1 answer
150 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". ...
user avatar
  • 283
2 votes
1 answer
1k 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)...
user avatar
  • 359
0 votes
1 answer
3k 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 ...
user avatar
0 votes
1 answer
650 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 >...
user avatar
  • 1,734
3 votes
1 answer
2k 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 ...
user avatar
49 votes
3 answers
28k 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 ...
user avatar
  • 9,057
2 votes
1 answer
378 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 ...
user avatar
0 votes
2 answers
2k 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 ...
user avatar
3 votes
1 answer
232 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 (...
user avatar
  • 213
1 vote
1 answer
43 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 ...
user avatar
  • 836
5 votes
1 answer
3k 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 ...
user avatar
1 vote
1 answer
3k 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....
user avatar
  • 621
1 vote
2 answers
915 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 ...
user avatar
  • 9,057