Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. pragma solidity ^0.4.23;
  2.  
  3. contract Casting {
  4.  
  5. function uint8toBytes32(uint8 value) public returns(bytes32) {
  6. /*
  7. input: 255
  8. output: 0x00000000000000000000000000000000000000000000000000000000000000ff
  9. */
  10. return bytes32(value);
  11. }
  12.  
  13. function uinttoBytes32(uint value) public returns(bytes32) {
  14. /*
  15. input: 255
  16. output: 0x00000000000000000000000000000000000000000000000000000000000000ff
  17. */
  18. return bytes32(value);
  19. }
  20.  
  21. function bytes32ToAddress(bytes32 provAdd) public returns(address) {
  22. /*
  23. input: 0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
  24. output: 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
  25. */
  26. return address(provAdd);
  27. }
  28.  
  29. function bytes32ToBytes16(bytes32 convBytes32) public returns(bytes16) {
  30. //Input bytes32
  31. //0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c
  32. //Output bytes16
  33. //0x000000000000000000000000ca35b7d9
  34. //Important Notice
  35. //16 bytes with length size of 32 from left is being included rest are excluded.
  36. return bytes16(convBytes32);
  37. }
  38.  
  39. function bytes8ToUint32(bytes8 value8) public returns(uint32) {
  40. //Study comment section below of two functions.
  41. return uint32(value8);
  42. }
  43.  
  44. function bytes8ToUint16(bytes8 value8) public returns(uint16) {
  45. /*
  46. Input bytes8 value: 0x73616e6465736872('sandeshr')
  47. Output uint16: 26738
  48.  
  49. Since uint16 only hold 2**16 - 1 integers, EVM proceed the conversion from right
  50. side, converting hexadecimal 0x6872 is equal to 114 any thing greater than than
  51. will result into being greater than integer 65536.
  52. */
  53. return uint16(value8);
  54. }
  55.  
  56. function bytes8ToUint8(bytes8 value8) public returns(uint8) {
  57. /*
  58. Input bytes8 value: 0x73616e6465736872('sandeshr')
  59. Output uint8: 114
  60.  
  61. Since uint8 only hold 2**8 - 1 integers, EVM proceed the conversion from right
  62. side, converting hexadecimal 0x72 is equal to 114 any thing greater than than
  63. will result into being greater than integer 255.
  64. */
  65. return uint8(value8);
  66. }
  67.  
  68. function retBytes8(bytes8 value8) public returns(bytes8) {
  69. return value8;
  70. }
  71.  
  72. function byte8ToByte16(bytes8 value8) public returns(bytes16) {
  73. /*
  74. bytes8 input: 0x73616e6465736872
  75. bytes16 output: 0x73616e64657368720000000000000000
  76. */
  77. return bytes16(value8);
  78. }
  79.  
  80. function uintToAddress(uint value256) public returns(address) {
  81. address(value256);
  82. }
  83. }
Add Comment
Please, Sign In to add comment