Bytes in Solidity


There 2 Types of bytes:

Fixed size bytes from (bytes1 to bytes32)

dynamic bytes (with specific length)

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract _bytes{
    //1 byte is equal to 8 bits
    //bytes store data in the form of hexadecimal
    //if we want to store a into bytes first a convet to its ascii which is 61
    //after that it store in bytes and 61 is in hexadecimal fom
    //meanwhile bytes store data in the form array. 
    //first index store 2 hexadecimal, similarly otherone more
    
    
    //these are fixed size array
    bytes3  public b3;
    bytes2  public b2;
    
    //this is bynamic bytes array
    bytes public dynamicBytes;
    
    function set()public {
        b3 = 'abc';
        //bytes is immutable means we can't change any index value
        b2 = 'ab';
    } 
    
    function checkK()public view returns(bytes32){
        bytes memory bb = 'asxasxs'; 
        return keccak256(bb);
    }
    
    function setdynamicBytes()public{
        dynamicBytes.push('d');
    }
    
    function getdynamicBytesByIndex(uint i)public view returns(bytes1){
        return dynamicBytes[i];
    }
}