solidity 十六进制字符串转十六进制bytes

pragma solidity ^0.4.16;


contract Metadata {

    // 十六进制字符串转换成bytes
    function hexStr2bytes(string data)returns (bytes){

        uint _ascii_0 = 48;
        uint _ascii_A = 65;
        uint _ascii_a = 97;

        bytes memory a = bytes(data);
        uint[] memory b = new uint[](a.length);

        for (uint i = 0; i < a.length; i++) {
            uint _a = uint(a[i]);

            if (_a > 96) {
                b[i] = _a - 97 + 10;
            }
            else if (_a > 66) {
                b[i] = _a - 65 + 10;
            }
            else {
                b[i] = _a - 48;
            }
        }

        bytes memory c = new bytes(b.length / 2);
        for (uint _i = 0; _i < b.length; _i += 2) {
            c[_i / 2] = byte(b[_i] * 16 + b[_i + 1]);
        }

        return c;
    }
}

solidity 智能合约 从公钥得到账户地址

function pk2account(address _address) public returns (address) {
        uint160 m = 0;
        uint160 tmp = 0;
        m = uint160(_address);
        tmp >>= 80;
        tmp <<= 80;
        m-=tmp;
        return address(m);
    }

转载于:https://www.cnblogs.com/bergus/p/solidity-shi-liu-jin-zhi-zi-fu-chuan-zhuan-shi-liu.html

你可能感兴趣的:(solidity 十六进制字符串转十六进制bytes)