Submission 6f1348c5...

ChallengeHex decoder
Submittermichaelsproul.eth
Submitted at2018-50-29
Gas used1460599
/**
 * 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/
 *
 * Implementation by Michael Sproul (https://sproul.xyz)
 */

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) {
        // 0 in ASCII is 48
        // A in ASCII is 65
        // a in ASCII is 97
        bytes memory inputBytes = bytes(input);
        uint n = inputBytes.length;
        uint m = n / 2;
        output = new bytes(m);
        uint b;
        uint c;
        for (uint i = 0; i < m; i++) {
            // ith byte from characters c = 2i and c = 2i + 1
            c = i + i;
            b = uint(inputBytes[c]);
            // 57 = '9'
            if (b < 58) {
                b = (b - 48) << 4;
            }
            // 70 = 'F'
            else if (b <  71) {
                b = (b - 55) << 4;
            } else {
                // a = 10
                b = (b - 87) << 4;
            }

            // Swap the order of the conditional the second time round
            c++;
            c = uint(inputBytes[c]);
            // 57 = '9'
            if (c < 58) {
                c = (c - 48);
            }
            // 70 = 'F'
            else if (c > 70) {
                c = (c - 87);
            } else {
                // a = 10
                c = (c - 55);
            }

            output[i] = byte(b | c);
        }

        return output;
    }
}