Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.23;
- contract Casting {
- function uint8toBytes32(uint8 value) public returns(bytes32) {
- /*
- input: 255
- output: 0x00000000000000000000000000000000000000000000000000000000000000ff
- */
- return bytes32(value);
- }
- function uinttoBytes32(uint value) public returns(bytes32) {
- /*
- input: 255
- output: 0x00000000000000000000000000000000000000000000000000000000000000ff
- */
- return bytes32(value);
- }
- function bytes32ToAddress(bytes32 provAdd) public returns(address) {
- /*
- input: 0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
- output: 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
- */
- return address(provAdd);
- }
- function bytes32ToBytes16(bytes32 convBytes32) public returns(bytes16) {
- //Input bytes32
- //0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
- //Output bytes16
- //0x000000000000000000000000ca35b7d9
- //Important Notice
- //16 bytes with length size of 32 from left is being included rest are excluded.
- return bytes16(convBytes32);
- }
- function bytes8ToUint32(bytes8 value8) public returns(uint32) {
- //Study comment section below of two functions.
- return uint32(value8);
- }
- function bytes8ToUint16(bytes8 value8) public returns(uint16) {
- /*
- Input bytes8 value: 0x73616e6465736872('sandeshr')
- Output uint16: 26738
- Since uint16 only hold 2**16 - 1 integers, EVM proceed the conversion from right
- side, converting hexadecimal 0x6872 is equal to 114 any thing greater than than
- will result into being greater than integer 65536.
- */
- return uint16(value8);
- }
- function bytes8ToUint8(bytes8 value8) public returns(uint8) {
- /*
- Input bytes8 value: 0x73616e6465736872('sandeshr')
- Output uint8: 114
- Since uint8 only hold 2**8 - 1 integers, EVM proceed the conversion from right
- side, converting hexadecimal 0x72 is equal to 114 any thing greater than than
- will result into being greater than integer 255.
- */
- return uint8(value8);
- }
- function retBytes8(bytes8 value8) public returns(bytes8) {
- return value8;
- }
- function byte8ToByte16(bytes8 value8) public returns(bytes16) {
- /*
- bytes8 input: 0x73616e6465736872
- bytes16 output: 0x73616e64657368720000000000000000
- */
- return bytes16(value8);
- }
- function uintToAddress(uint value256) public returns(address) {
- address(value256);
- }
- }
Add Comment
Please, Sign In to add comment