I'll admit this isn't the most elegant solution, but it works perfectly in IDE, including with binary numbers whose lengths are not multiples of 4. When I try to validate in CodeGym, though, I get a "String index 0 out of range" error in the toHex method, which shouldn't be possible.