Questions tagged [type-casting]
Type casting is the act of converting a value from one data type to another in a computer program.
37
questions
1
vote
1
answer
137
views
Grab first 20 bytes of 32-bytes input
In solidity casting or assembly, how do I grab the first 20 bytes of this 32-bytes input: 0x00010000c900c90062fceaad00000000000000000002af416812f9bb6f2fdff5?
Thanks!
1
vote
1
answer
21
views
How to create a hash based on struct array types?
I have the following arguments to call a function in a smart contract:
ERC20Reward[] calldata _erc20Rewards
ERC721Reward[] calldata _erc721Rewards
ERC1155Reward[] calldata _erc1155Rewards
...
0
votes
0
answers
64
views
Dynamically parsing the Ethereum logs returned from FilterLogs function
I am trying to parse the logs of a contract in a fully dynamic manner. Meaning that the only thing I have is the abi of the contract, and the filter query to filter out logs. I would like to parse the ...
0
votes
0
answers
38
views
To hash or not to hash strings for bytes32 AccessControl roles
Is there a preferred/safest way to declare roles for Openzeppelin's AccessControl.sol?
I would imagine a straight assignment of a bytes32 from a string would be sufficient for role creation as long as ...
1
vote
1
answer
109
views
object returned for uint256 return value
I implemented a method to retrieve a price for a currency via some chainlink price feed. This price is casted to an uint256. But if I call this method via etherscan it does not show a number, but just ...
0
votes
1
answer
239
views
Solidity 0.8.0 - explicit type conversion of address to bytes
I am trying to convert an address to bytes so I can perform bitwise operations on the value but noticed that there was a breaking change in version 0.8.0. How would I to modify this code to perform ...
0
votes
2
answers
193
views
How can I call a function which needs string calldata as input parameter?
I have a function which has an input parameter as string calldata. I don't know how to pass a string to this function. The simple solution gives me a compile error as it cannot convert a ...
2
votes
2
answers
290
views
What is standard practice to cast msg.sender to "payable"?
Introduction
I am working on a bounty contract. Here is the function for somebody answering the bounty. I am trying to figure out when it is better to cast to payable, it seems intuitively that always ...
1
vote
2
answers
321
views
Get first digit of number [closed]
In Solidity, how do you get the first digit of a two-digit number, like 65 for example?
0
votes
1
answer
369
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 ...
6
votes
1
answer
3k
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/...
2
votes
2
answers
112
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 ...
1
vote
2
answers
861
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 ...
0
votes
1
answer
604
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(...
1
vote
1
answer
1k
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 ...
1
vote
1
answer
1k
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.
...
1
vote
1
answer
599
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 ...
1
vote
2
answers
372
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
...
0
votes
1
answer
419
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 ...
0
votes
0
answers
281
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 ...
0
votes
1
answer
986
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 ...
2
votes
2
answers
5k
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: ...
1
vote
2
answers
602
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)...
1
vote
1
answer
203
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".
...
2
votes
1
answer
2k
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
1
answer
4k
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
1
answer
857
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 >...
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 ...
62
votes
3
answers
38k
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 ...
2
votes
1
answer
522
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 ...
0
votes
2
answers
3k
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 ...
3
votes
1
answer
287
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
1
answer
51
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 ...
5
votes
1
answer
4k
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 ...
1
vote
1
answer
4k
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....
1
vote
2
answers
2k
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 ...
14
votes
1
answer
16k
views
How to convert a uint256 type integer into a bytes32?
Since both types, uint256's and bytes32's space are 2^256. How to map an uint256 type integer into bytes32 type in Solidity ?