Safer/better implementation of hex_to_int