Skip to content

Instantly share code, notes, and snippets.

@lyhistory
Created January 30, 2019 09:31
Show Gist options
  • Save lyhistory/af1a486fa6ac8464ac5f850ac7404ef0 to your computer and use it in GitHub Desktop.
Save lyhistory/af1a486fa6ac8464ac5f850ac7404ef0 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.0 <0.6.0;
contract ConversionExample {
function test() public {
int8 y = -3;
uint x =uint(y); //0xfffff..fd
uint32 a = 0x12345678;
uint16 b = uint16(a);// b will be 0x5678 now
uint16 a2 = 0x1234;
uint32 b2 = uint32(a2);
// b will be 0x00001234 now
assert(a2 == b2);
bytes2 a3 = 0x1234;
bytes1 b3 = bytes1(a3);// b will be 0x12
bytes2 a4 = 0x1234;
bytes4 b4 = bytes4(a4);// b will be 0x12340000
assert(a4[0] == b4[0]);
assert(a4[1] == b4[1]);
//Since integers and fixed-size byte arrays behave differently when truncating or padding, explicit conversions between
//integers and fixed-size byte arrays are only allowed, if both have the same size. If you want to convert between integers
//and fixed-size byte arrays of different size, you have to use intermediate conversions that make the desired truncation
//and padding rules explicit
bytes2 a5 = 0x1234;
uint32 b5 = uint16(a5);// b will be 0x00001234
uint32 c5 = uint32(bytes4(a5));// c will be 0x12340000
uint8 d5 = uint8(uint16(a5));// d will be 0x34
uint8 e5 = uint8(bytes1(a5));// e will be 0x12
//Decimal and hexadecimal number literals can be implicitly converted to any integer type that is large enough to
//represent it without truncation:
uint8 a6 = 12; //fine
uint32 b6 = 1234; //fine
//uint16 c6 = 0x123456; //fails, since it would have to truncate to 0x3456
//Decimal number literals cannot be implicitly converted to fixed-size byte arrays. Hexadecimal number literals can
//be, but only if the number of hex digits exactly fits the size of the bytes type
bytes2 a7 = 54321; // not allowed
bytes2 b7 = 0x12; // not allowed
bytes2 c7 = 0x123; // not allowed
bytes2 d7 = 0x1234; // fine
bytes2 e7 = 0x0012; // fine
bytes4 f7 = 0; // fine
bytes4 g7 = 0x0;// fine
//String literals and hex string literals can be implicitly converted to fixed-size byte arrays, if their number of characters
//matches the size of the bytes type:
bytes2 a8 = hex"1234";// fine
bytes2 b8 = "xy";// fine
bytes2 c8 = hex"12"; // not allowed
bytes2 d8 = hex"123"; // not allowed
bytes2 e8 = "x"; // not allowed
bytes2 f8 = "xyz"; // not allowed
//As described in Address Literals:
//Hexadecimal literals that pass the address checksum test, for example 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF
//are of address payable type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the
//checksum test produce a warning and are treated as regular rational number literals.
//, hex literals of the correct size that pass the checksum test are of address type.
//No other literals can be implicitly converted to the address type.
//Explicit conversions from bytes20 or any integer type to address result in address payable.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment