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
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!
dNyrM's user avatar
  • 711
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 ...
Falcon Stakepool's user avatar
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 ...
Ahmet Yazıcı's user avatar
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 ...
JasonGarcia.eth's user avatar
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 ...
jan's user avatar
  • 111
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 ...
AAMCODE's user avatar
  • 133
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 ...
Azzurro94's user avatar
  • 111
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 ...
Victor Forissier's user avatar
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?
dNyrM's user avatar
  • 711
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 ...
Luke Hutchison's user avatar
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/...
dNyrM's user avatar
  • 711
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 ...
Paul Razvan Berg's user avatar
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 ...
MShakeG's user avatar
  • 1,408
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(...
MShakeG's user avatar
  • 1,408
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 ...
alper's user avatar
  • 8,368
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. ...
João Abrantes's user avatar
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 ...
Lin's user avatar
  • 123
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 ...
penguin's user avatar
  • 133
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 ...
Manas Nanivadekar's user avatar
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 ...
ali's user avatar
  • 41
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 ...
Tristan Nemoz's user avatar
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: ...
Maifee Ul Asad's user avatar
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)...
Jung Chun's user avatar
  • 191
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". ...
okwme's user avatar
  • 293
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)...
Kristaps's user avatar
  • 359
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 ...
Stef Heyenrath's user avatar
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 >...
Briomkez's user avatar
  • 1,884
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 ...
Paul Razvan Berg's user avatar
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 ...
Jesbus's user avatar
  • 10.3k
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 ...
Sunbeom So's user avatar
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 ...
Prabhanjan Naib's user avatar
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 (...
4ntoine's user avatar
  • 213
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 ...
s k's user avatar
  • 886
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 ...
Gustavo Alvarez's user avatar
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....
Senju's user avatar
  • 691
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 ...
Jesbus's user avatar
  • 10.3k
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 ?
Wang's user avatar
  • 2,396