Submission 8ad120c2...

ChallengeHex decoder
Submittersggc.yuetloo.eth
Submitted at2018-39-03
Gas used1502890
/**
 * This contract was a modified version of the original from 
 * https://github.com/arachnid/sggc.git
 *
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 */

pragma solidity 0.4.24;

contract HexDecoder {

    /**
     * @dev Decodes a hex-encoded input string, returning it in binary.
     *
     * Input strings may be of any length, but will always be a multiple of two
     * bytes long, and will not contain any non-hexadecimal characters.
     *
     * @param input The hex-encoded input.
     * @return The decoded output.
     */

    function decode(string input) public pure returns(bytes output) {

        output = new bytes(bytes(input).length / 2);

        uint j= 0;
        uint len = output.length;
        for(uint i = 0; i < len; i++) {
           uint b1 = uint(bytes(input)[j]);
           uint b2 = uint(bytes(input)[j+1]);
           output[i] = byte(((b1 < 0x40? (b1 & 0x0f) : (b1 & 0xf) + 9) * 16)
                           | (b2 < 0x40? (b2 & 0x0f) : (b2 & 0xf) + 9));
           j+=2;
        }
    }
}