05.04

조하빈 ·2023년 5월 4일
0

05월 04일 목요일


Solidity -uint

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;
contract UINT {
    uint a;
    uint8 b; // 8bits = 1 byte = 16진수 2자리 = 0 ~ ff(16진수) = 0 ~ 255(10진수)
    uint16 c;
    uint256 d;
    function setABCD(uint _a, uint8 _b, uint16 _c, uint256 _d) public {
        a = _a;
        b = _b;
        c = _c;
        d = _d;
    }
    function setABCD2(uint _a, uint8 _b, uint8 _c, uint8 _d) public {
        a = _a; // 준비한 것은 64자리, 들어온것은 2자리 -> 문제없음
        b = _b; // 준비한 것은 2자리, 들어온것은 2자리 -> 문제없음
        c = _c; // 준비한 것은 4자리, 들어온것은 2자리 -> 문제없음
        d = _d; // 준비한 것은 64자리, 들어온것은 2자리 -> 문제없음
    }
    /*
    function setABCD3(uint256 _a, uint256 _b, uint256 _c, uint256 _d) public {
        a = _a;
        b = _b; // uint8로 선언되었는데 들어오는 값은 더 큰 uint256
        c = _c; // uint16로 선언되었는데 들어오는 값은 더 큰 uint256
        d = _d;
    }
    */
    function getABCD() public view returns(uint, uint8, uint16, uint256) {
        return (a,b,c,d);
    }
    function getABCD2() public view returns(uint, uint, uint, uint) {
        return (a,b,c,d);
    }
    /*
    function getABCD2() public view returns(uint8, uint8, uint8, uint8) {
        return (a,b,c,d);
        a, d // 준비한 것은 uint256인데, 내보내는 값은 uint8 밖에 안됨 -> 준비한 값의 일부 날라감
    }
    */
}

Solidity - bytes

/*
실습가이드
1.0x10, 0x11, 0x12 넣어서 setABC 해보기 -> 오류 확인 incorrect data length
2.0x10, 0x11, 0x0012 넣어서 setABC 해보기
3.0xa665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
4.getABCD결과 확인
*/ 
contract BYTES {
    bytes a; //사실상 무한정 -> 자리수 동적
    bytes1 b; // 자리수 정적(결정됨) => 16진수 2자리
    bytes2 c; // 자리수 정적(결정됨) => 16진수 4자리
    bytes32 d; // 자리수 정적(결정됨) => 16진수 64리    
    //bytes 는 special array라서 앞에 memory를 붙여줘야함 하지만 bytes1,2 는 크기가 정해져있기 때문에 적어줄 필요가 없다
    function setABC(bytes  memory _a, bytes1  _b, bytes2  _c) public {
        a= _a; 
        b= _b;
        c= _c;
    }
    //bytes memory와 bytes32는 엄연히 다르다 bytes memory는 자리수를 최대한 많이 준비해놓기때문에 고로 d에 bytes memory 를 적으면 오류가 난다.
     function setABC2(bytes  memory _a, bytes1  _b, bytes2 _c, bytes32 _d) public {
        a= _a;
        b= _b;
        c= _c;
        d= _d;
    }
    function getABC() public view returns(bytes memory, bytes1, bytes2, bytes32){
        return (a,b,c,d);
    }
}

Solidity - String

contract StringAndBytes {
    string a;
    function setSting(string memory _a) public {
        a = _a;
    }
    function getString() public view returns(string memory) {
        return  a;        
    }        //특정 문자를 뽑아서 사용하고싶을 때? 사용
    function stringToBytes() public view returns(bytes memory){
        return bytes(a);
    }
    function stringToBytes2() public view returns(bytes1, bytes1, bytes1) {
        return (bytes(a)[0], bytes(a)[1],bytes(a)[2]);
    }
    function bytesToString(bytes memory _a) public pure returns(string memory){
        return string (_a);
    }
        function bytesToString2(bytes memory _a) public pure returns(string memory){
        return string (_a);
    }
}

Solidity - Array

contract Array{
    // array - 같은 자료형이 들어있는 집합
    uint[] numbers; // 자료형 [] array 이름
    string[] letters;
    //1.넣기
    function push(uint _a) public {
        numbers.push(_a);
    }
    //2.빼기
    function pop() public {
        numbers.pop();
    }
    //3.보기
    function getNumber(uint _n) public view returns(uint) {
        return numbers[_n-1]; //배열이름[n번째]
    }
    //4.길이
    function getLength() public view returns(uint){
        return numbers.length;
    }
    //5.바꾸기
    function changeNum(uint _k, uint _z) public{
        numbers[_k-1] = _z;
    }
    //6. 삭제
    function DeleteNum(uint _n) public{
        delete numbers[_n];
    }
    //7. 전체 array반환
    function returnArray() public view returns(uint[] memory){
        return numbers;
    }
}

Solidity

    //_a의 첫번째 글자를 뽑아내는것이 목표
    // _a의 첫번째 글자를 바이트로 표현
        function bytesToString2(string memory _a) public pure returns(bytes1){
        bytes memory _b;
        _b = bytes(_a);
        return ( _b[0]);
    }
    // _a의 첫번째 글자를 뽑아내기
    function bytesToString3(string memory _a) public pure returns(string memory){
        bytes memory _b = new bytes(1);
        _b[0] = bytes(_a)[0];
        return string(_b);
    }
    // _a의 원하는 글자를 
    function bytesToString4(string memory _a, uint _n) public pure returns(string memory){
        bytes memory _b = new bytes(1);
        _b[0] = bytes(_a)[_n-1];
        return string(_b);
    }
    function bytesToString5(string memory_a) public pure returns(string memory){
    bytes memory _b = new bytes(1);
    _b[0] = bytes(_a)[0];
    return string(_b);
]
<hr/>

> ## Solidity -struct

contract Struct {
struct Student {
string name;
string gender;
uint number;
uint birth;
//구조체 선언 struct 구조체명 {.../.../...}
}
Student s; //Student형 변수 s
Student[] students;
function setStudent(string memory _name,string memory gender, uint _number, uint _birth) public {
s = Student(_name, gender,_number, _birth);
}
function getStudent() public view returns(Student memory){
return s;
}

function pushStudent(string memory _name, string memory _gender, uint _number, uint _birth) public {
    students.push(Student(_name, _gender, _number, _birth));
}
function popStudent() public {
    students.pop();
}
function getLength() public view returns(uint){
    return students.length;
}
function getStudent(uint _n) public view returns(Student memory){
    return (students[_n]);
}
function getStudents() public view returns(Student[] memory){
    return students;
}

}


<hr/>

> ## Solidity - Error
함수명이 같아도 에러가 발생하지않는다. 대신 인풋값의 갯수가 같으면 구분하지못한다.

<hr/>

각종 팁 :
- operating system
- data structure
- algorithm
컴퓨터 공학 커리큘럼
유명한 대학교들 무료강의 들어볼것 (?)
http://www.kocw.net/home/index.dohttp://www.kmooc.kr/
http://www.kmooc.kr/

- 16진수 넣는 법 : 0x로 시작

profile
PPisland

0개의 댓글