ethereum(以太坊)(十)--字节数组

pragma solidity ^0.4.0;

contract byte1{
    /*
    固定大小字节数组(Fixed-size byte arrays)
    固定大小字节数组可以通过bytes1,bytes2...bytes32声明,byte=byte1
    bytes1 只能存储1个字节,也就是二进制的8位内容   //一个字节=8位2进制/一个字母/符号/(1/3汉字)
    bytes2 只能存储2个字节,也就是二进制的8*2位内容
    bytes32 只能存储32个字节,也就是二进制的8*32=256位内容
    十六进制:1 2 3 4 5 6 7 8 9 a b c d e f
    */
    bytes1 a1=0x63; //0110 0011
    //byte   a2=0x632; Type int_const 1586 is not implicitly convertible to expected type bytes1

    bytes1 b01 = 0x6c; //0110 1100  10->16  12*16**0 +6*16**1 = 108
    bytes1 b11 = 0x69; //0110 1001  10->16  9*16**0 +6*16**1  = 105

    //比较操作符 <= , <,>=,>,==,!=

    function max() constant returns(bool){
        return b01 >= b11; //true
    }

    //位操作符 &,|,^,~,>>,<<
    function test1() constant returns(bytes1){
        //return b01 | b11;
        /*
        0110 1100
        0110 1001
        0110 1101 ->0x6d
        */
        return b01 ^ b11;
        /*
        0110 1100
        0110 1001
        0000 0101 -> 0x05
        */
    }
    //0x03 13 23 33 43 53 63 73 83
    //  0   1  2  3  4  5  6  7  8
    bytes9 b9 =0x031323334353637383;
    function testindex() constant returns(uint){
        //return b9[4]; //0x43
        return b9.length; //9  .length onlyread
    }
    //不可变数组 (长度不可变:b.length|内部存储的数据不可变:bytes[0])
    /*
    function setlen(uint8 a){ //TypeError: Expression has to be an lvalue
        b9.length = a;
    }

    function setvalue(byte a){ //TypeError: Expression has to be an lvalue
        b9[0] = a;
    }
    */

    bytes9 public g = 0x6c697975656368756e; //liyuechun

    string public name =‘liyuechun‘;//动态字符串数组

    function gByteLength() constant returns(uint){
        return g.length;
    }
    function stingToBytes() constant returns(bytes){
        return bytes(name);//还是string类型的动态字节数组
    }
    function setgByteLength(uint alen){
        bytes(name).length = alen;
    }

    function setgByteValue(bytes1 z){
        bytes(name)[0]= z;
    }
}

pragma solidity ^0.4.0;

contract byte1{
    bytes public b = new bytes(1); //bytes  声明动态大小字节数组
    //bytes(1)=0x00 bytes(2)=0x0000  定义动态字节数组长度
    //bytes public b = bytes(1); TypeError: Explicit type conversion not allowed from "int_const 1" to "bytes storage pointer"

    function setLeng(uint a) public{ //3 0x000000 |0 0x
        b.length = a;
    }

    function setValus(byte d,uint c ) public{ //0x06,0
        b[c] = d;
    }

    function setclear() public{
        delete b;
    }
}

原文地址:https://www.cnblogs.com/eilinge/p/9970747.html

时间: 2024-07-31 03:34:12

ethereum(以太坊)(十)--字节数组的相关文章

ethereum(以太坊)(十四)--Delete

pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长度变为0 bytes32:重置所有索引的值 mapping:什么都不会发生 mapping(key=>value)中的key:删除与该键相关的值 */ string public str1 ='nihao'; function deletstr() public{ delete str1; } fu

ethereum(以太坊)(九)--global(全局函数)

pragma solidity ^0.4.0; contract modifierTest{ bytes32 public blockhash; address public coinbase; uint public difficulty; uint public gaslimit; uint public blockNum; uint public timestamp; bytes public calldata1; uint public gas; address public sende

ethereum(以太坊)(五)--Bool

pragma solidity ^0.4.0; contract Bool{ uint num1 = 100; uint num2 = 200; bool _c = true; // && == function yuf() constant returns(bool){ return num1 == num2 && _c; //false } // || != function huof() constant returns(bool){ return num1 != n

ethereum(以太坊)(六)--整型(int)

/* uint8 uint16 ...uint256 int8 int16 int24 ..int256 uint => uint256 int => int256 int8 有符号 +1 ,-3 uint 无符号 1,2 int8 8(位) 1 111 1111 ~ 0 111 1111 - (1+2+4+8+16+32+64) ~ + (1+2+4+8+16+32+64) -127 ~ 127 255(127*2+0) uint8 0000 0000 ~ 1111 1111 (0~255)

ethereum(以太坊)(基础)--容易忽略的坑(一)

pragma solidity ^0.4.0; contract base{ address public _owner=msg.sender; uint _a; string internal _b ; uint private _c; //uint external _d;ParserError: Expected identifier but got 'external' function base() { _a =10; //uint _d public;Expected ';' but

ethereum(以太坊)(基础)--容易忽略的坑(二)

pragma solidity ^0.4.0; contract EMath{ string public _a="lin"; function f() public{ modify(_a); } //function modify(string storage name) private{ function modify(string memory name) private{ //string storage c = name;Type string memory is not i

(转)写文章 以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘

什么是以太坊(Ethereum) 以太坊(Ethereum)是一个基于区块链技术,允许任何人构建和使用去中心化应用的区块链平台.像比特币一样,以太坊是开源的,并由来自全世界的支持者们共同维护.与比特币仅提供了有限功能的脚本不同,以太坊提供了一个"图灵完备"的虚拟机,称为以太坊虚拟机(Ethereum Virtual Machine),简称EVM,用户可以在EVM上创建智能合约(Smart Contract).以太坊平台中的通用货币为以太币(Ether),简称ETH,以太币可用于账号间的

以太坊是什么 - 以太坊开发入门指南

很多同学已经跃跃欲试投入到区块链开发队伍当中来,可是又感觉无从下手,本文将基于以太坊平台,以通俗的方式介绍以太坊开发中涉及的各晦涩的概念,轻松带大家入门. 以太坊是什么 以太坊(Ethereum)是一个建立在区块链技术之上, 去中心化应用平台.它允许任何人在平台中建立和使用通过区块链技术运行的去中心化应用. 对这句话不理解的同学,姑且可以理解为以太坊是区块链里的Android,它是一个开发平台,让我们就可以像基于Android Framework一样基于区块链技术写应用. 在没有以太坊之前,写区

以太坊开发完整去中心化应用 —— 区块链投票系统

第一节 课程概述 本课程面向初学者,内容涵盖以太坊开发相关的基本概念,并将手把手地教大家如何构建一个 基于以太坊的完整去中心化应用 -- 区块链投票系统. ethereum logo 通过本课程的学习,你将掌握: 以太坊区块链的基本知识 开发和部署以太坊合约所需的软件环境 使用高级语言(solidity)编写以太坊合约 使用NodeJS编译.部署合约并与之交互 使用Truffle框架开发分布式应用 使用控制台或网页与合约进行交互 前序知识要求 为了顺利完成本课程,最好对以下技术已经有一些基本了解