Decimals and Hex Literals Assignments uintN

In Solidity, we can assign values using decimal and hexadecimal literals to uintN types. These assignments require explicit conversion when assigning to smaller uint types.

Solidity




// Solidity program for Decimals 
// and Hex Literals Assignments uintN
pragma solidity ^0.8.0;
  
contract Conversion {
    
  // Decimal literal assignment
  uint8 public a = 42; 
    
  // Hexadecimal literal assignment
  uint256 public b = 0x123456789ABCDEF; 
}


Output:

 

Explicit Conversions in Solidity

Converting data types in Solidity requires explicit conversions. When working with incompatible data formats, this is beneficial. Solidity explicitly converts values via casting operations. To maintain compatibility or execute a particular action, Solidity data types might have distinct sizes and representations. 

Similar Reads

What is Explicit Conversion?

Explicit conversions may convert simple kinds like integers and floating-point numbers or complicated types like arrays and structs. Solidity supports typecasting, type conversion via a function Object() { [native code] }, and conversion using built-in conversion methods like address(uint160(addr)) or string(addr) (data)....

Explicit Conversion vs Implicit Conversion

Implicit conversion happens when a value of one type is given to a variable of another type, unlike explicit conversion.  Implicit conversions in Solidity are restricted to converting an unsigned integer to a bigger unsigned integer or a string literal to a bytes type. Explicit conversions may convert more types....

Unsigned integer conversion

Solidity supports uint8, uint16, uint32, uint64, uint128, and uint256. These types may be converted explicitly....

Conversion between bytesN

...

Conversions from uintM to bytesN

...

Conversion from bytesN to uintM

Solidity also supports fixed-size byte arrays, such as bytes8, bytes16, bytes32, and so on. Explicit conversions can be used to convert between these types when necessary....

Decimals and Hex Literals Assignments uintN

...

Conversion from bytes to bytesN

Solidity supports converting unsigned integers to bytesN types with explicit conversion, where N is a multiple of 8 between 1 and 32....

Conversion between bytes and strings

...

Conversions to address Type

Solidity supports converting bytesN types to unsigned integers with explicit conversion, where N is a multiple of 8 between 1 and 32....

Conversion between address and address payable

...

Conversion between Contract and address types

In Solidity, we can assign values using decimal and hexadecimal literals to uintN types. These assignments require explicit conversion when assigning to smaller uint types....