pragma solidity ^0.4.16;
contract HelloWorld {
string myName = "xujialu,helloworld";
bytes1 num1 = 0x12;
bytes2 num2 = 0x1212;
bytes3 public num3=0x123456;
bool flag = false;
string testword = "xujialu"; //0x78756a69616c75
string testword1='asldhlkasdh';
string testword2='^*^*%*()*-/';
string testword3='中文测试';
string testword4='中文测试2222';
function stringLength() public view returns(uint){
// return testword.length 直接返回string类型的字符串legth会报错,因为string没有这个方法
// length这个方法bytes中有
return bytes(testword).length;
}function stringjinzhi() public view returns(bytes)
{
return bytes(testword); //stringchange执行后十六进制0x41756a69616c75;
}
function stringchange() public {
// testword[0]="A";这样会直接报错,string类型的不能直接改变
//借助bytes
bytes(testword)[0]="A";
}
function stringchangtest() public view returns(bytes1){
return bytes(testword)[0]; // 原来 0x78756a69616c75 现在 0x41756a69616c75 0x41
}
//string 类型是怎么存储字符串的,对于字母和一般特殊字符,都是一个字符占一个字节
//对于中文字符是一个字符占3个字节
function stringjinzhitest1() public view returns(uint,bytes){
return (bytes(testword1).length,bytes(testword1));
}
function stringjinzhitest2() public view returns(uint,bytes){
return (bytes(testword2).length,bytes(testword2));
}
function stringjinzhitest3() public view returns(uint,bytes){
return (bytes(testword3).length,bytes(testword3));
}
function stringjinzhitest4() public view returns(uint,bytes){
return (bytes(testword4).length,bytes(testword4));
}
//固定长度字节数组bytes的截断
//结论:位数足够则保留前面的,位数不够再后面加0
bytes10 testword10=0x68656c6c6f776f726c64; //helloworld
function transbytes1() public view returns(bytes1){
return bytes1(testword10); // 0x68
}
function transbytes2() public view returns(bytes5){
return bytes5(testword10); // 0x68656c6c6f
}
function transbytes3() public view returns(bytes12){
return bytes12(testword10); //0x68656c6c6f776f726c640000
}
// view pure
function getName() public view returns(string){
return myName;
}
// + - * / yunsuan
function chengTest(uint a,uint b) pure public returns(uint){
return a*b;
}
function addTest(uint a,uint b) pure public returns(uint){
return a+b;
}
function jianTest(uint a,uint b) pure public returns(uint){
return a-b;
}
// bool && ||
function boolTest (bool a) view public returns (bool){
return flag && a;
}
function boolTest1 (bool a) view public returns (bool){
return flag || a;
}
function boolTest2 (bool a) view public returns (bool){
return flag == a;
}
// ** %
function quyu (uint a,uint b) pure public returns (uint){
return a%b;
}
function miTest (uint a,uint b) pure public returns (uint){
return a**b;
}
//weiyunsuan
function Wyutest(uint8 a,uint8 b) public pure returns(uint8){
return a&b;
}
function Whuotest(uint8 a,uint8 b) public pure returns(uint8){
return a|b;
}
function Wfantest(uint8 a) public pure returns(uint8){
return ~a;
}
function Wyihuotest(uint8 a,uint8 b) public pure returns(uint8){
return a^b;
}
function zuoyitest(uint8 a,uint8 b) public pure returns(uint8){
return a<<b;
}
function youyitest(uint8 a,uint8 b) public pure returns(uint8){
return a>>b;
}
function getLength() public view returns (uint8,uint16){
return (num1.length,num2.length);
}
}