Submission df69394a...

ChallengeHex decoder
Submittersggc.yuetloo.eth
Submitted at2018-07-03
Gas used1550231
/**
 * 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 v = (uint(bytes(input)[j]) * 256) | uint(bytes(input)[j+1]);
           v = (v & 0x0f0f) + (((((v & 0xf0f0)/16) & 0x0505) + 0x0505) & 0x0909);
           output[i] = byte( ((v&0x0f00)/16) | (v&0x0f));
           j+=2;
        }
    }
}