05-23

조하빈 ·2023년 5월 23일
0

05월 23일 화요일


Solidity Concat

  1. a,b 를 넣었을 때 ab로 합쳐지게 하는함수
    function concat1(string memory a, string memory b) public pure returns(string memory ){
        return string(abi.encodePacked(a,b));
    }
    function concat2(string memory a, string memory b) public pure returns(string memory){
        return string.concat(a,b);
    }
  1. uint a를 넣었을 때에는 아스키코드를 따라서 값을 가져온다.
   function concat3(uint a, string memory b, string memory c) public pure returns(string memory) {
        return string (abi.encodePacked(a,b,c));
    }
  1. 123을 나타내고 싶을 때
 function concat4(uint a, uint b, uint c) public pure returns(string memory) {
           a = a+48; // 48을 더해주는 이유는 아스키코드 숫자번호가 48부터 0~...으로 시작하기 때문
           b = b+48;
           c  = c+48;
        return string(abi.encodePacked(a,b,c)); // 위의 ab합치는것과 같은 원리
    }
  1. 시간초를 시분초로 나타내는 함수
    function Time(uint a) public pure returns(uint, uint, uint){
    uint hour;
    uint min;
    uint sec;
    hour = a / 3600 ;
    min = a/60%60;
        sec =a%60;
        return (hour,min,sec);
    }
  1. 숫자의 자릿수와 각 자리를 쪼개는 함수
 function divideNumber(uint _n) public pure returns(uint[] memory) {
        uint[] memory b = new uint[](getLength(_n));
        uint i=getLength(_n);
        while(_n !=0) {
            b[--i] = _n%10;
            _n = _n/10;
        }
        return (b);
    }
    function getLength(uint _n) public pure returns(uint) {
        uint a;
        while(_n > 10**a) { 
            a++;
        }
        return a;
    }
  1. 4,5번을 합쳐서 각 시간의 뒤에 hours,min,sec을 붙이는 함수
Quiz q7 = new Quiz(); <= quiz컨트랙트를 가져와 인스턴스 화 시켜준 후 time함수 사용
Q5 q5 = new Q5(); <= 위와 마찬가지로 divideNumber함수를 사용하기 위해 다른 컨트랙트 가져옴
    function concat7(uint _n) public view returns(string memory) {
        (uint a, uint b, uint c) = q7.Time(_n);
        return string(abi.encodePacked(concat5(a),"hours ", concat5(b), "minutes ", concat5(c), "seconds"));
    }
profile
PPisland

0개의 댓글