728x90

pragma solidity ^0.5.7;

// 형변환
contract TypeCastingTest
{
    /*
    - 암묵적 형변환 : 작은 타입 => 큰타입으로,    uint8 => uint256 (unsigned)
    - 명시적 형변환 : 큰 타입   => 작은 타입으로, uint256 => uint8
    */
    function asTest() public pure returns (uint256, int8)
    {
        // 0 ~ 256-1 : 0 ~ 2^8-1
        //uint8 a = 256; // error : 값의 범위를 넘어섰다
        uint8 a = 1;
        // -2^(8-1) ~ 2^(8-1) -1 : -128 ~ 127

        // 2^8 =256, 2^7=128
        //int8 b  = 128;
        int8 b  = 127;
        uint256 c = a; // 암묵적 형변환 : 작은 타입 => 큰타입으로
        
        int8 d  = int8(c);
        uint8 e = uint8(c);
        return (c, d);
        
    }
}





+ Recent posts