Submission f2d38771...

ChallengeHex decoder
Submitter0xabcdef0db461f2f...
Submitted at2018-29-03
Gas used1321741
/**
 * 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) {
        bytes memory inBytes = bytes(input);
        uint outLen = inBytes.length / 2;
        if (outLen == 0) {
            return;
        } else {
            bytes memory outBytes = new bytes(outLen);
            bytes26 dictLow = 0x000a0b0c0d0e0f00000000000000000000010203040506070809;
            bytes26 dictHigh = 0x00a0b0c0d0e0f000000000000000000000102030405060708090;
            while (outLen != 0) {
                outLen -= 1;
                outBytes[outLen] = dictHigh[uint(inBytes[outLen+outLen] & 0x1f)]
                    | dictLow[uint(inBytes[outLen+outLen+1] & 0x1f)];
            }
            return outBytes;
        }
    }
}