pragma solidity 0.5.17;
contract PublicFunction {
//公共除錯工具庫部分
//列印日志的模塊
event LogUint(string , uint);
function log(string memory s , uint x) internal {
emit LogUint(s, x);
}
event LogInt(string , int);
function log(string memory s , int x) internal {
emit LogInt(s, x);
}
event LogBytes(string, bytes);
function log(string memory s , bytes memory x) internal {
emit LogBytes(s, x);
}
event LogBytes32(string, bytes32);
function log(string memory s , bytes32 x) internal {
emit LogBytes32(s, x);
}
event LogAddress(string, address);
function log(string memory s , address x) internal {
emit LogAddress(s, x);
}
event LogBool(string, bool);
function log(string memory s , bool x) internal {
emit LogBool(s, x);
}
//呼叫列印log日志
function test() public {
log("testtest",true);
}
//bytes 和 string可以轉換;如果是byte1...轉換成string,則首先要轉換成bytes,再轉換成string.
function test1 () public{
string memory str1 = "ssssss";
bytes memory byteValue1 = new bytes(2);
bytes4 byteValue2 = 0x77881122;
uint ddd =111;
byteValue1[0] = 0x66;
//位元組動態位元組陣列轉換成字串
str1 = string(byteValue1);
//字串抓換成位元組動態位元組陣列
byteValue1 = bytes(str1);
//固定位元組陣列轉換成動態位元組陣列
bytes memory byteValue3 = new bytes(byteValue2.length);
for (uint i= 0 ; i< byteValue2.length ;i ++ ){
byteValue3[i] = byteValue2[i];
}
byteValue1 = bytes(byteValue3);
}
//位元組動態位元組陣列轉換成uint 256
function bytesToUint(bytes memory b) public view returns (uint256){
uint256 number;
for(uint i= 0; i<b.length; i++){
number = number + uint8(b[i])*(2**(8*(b.length-(i+1))));
}
return number;
}
// string型別轉化為bytes32型轉
function stringToBytes32(string memory source) public returns(bytes32 result){
assembly{
result := mload(add(source,32))
}
//呼叫列印log日志
log("stringToBytes32",result);
}
//bytes建構式bytes位元組輸入格式
//["0x616e7a68697169616e6700000000000000000000000000000000000000000000","0x616e6c6569000000000000000000000000000000000000000000000000000000"]
//bytes32型別轉化為string型轉
// Bytes32ToString
function NewBytes32ToString(bytes32 bname) public returns(string memory){
// 此處要加上memory
// 先將有效字符計算出來
bytes memory bytesChar = new bytes(bname.length);
uint charCount = 0;
for(uint i = 0;i < bname.length; i++){
bytes1 char = bname[i];
if(char != 0){
charCount++;
}
}
// 新建陣列,指定長度為有效位元組長度
bytes memory bytesName = new bytes(charCount);
for(uint j = 0;j < charCount;j++){
bytesName[j] = bname[j];
}
return string(bytesName);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335237.html
標籤:區塊鏈
上一篇:IPFS怎么瀏覽存盤的檔案
下一篇:fabric 簡單理解
