Surjection from real numbers to complex numbers

I'm having trouble coming up with an easy-to-describe surjection $f : \mathbb{R} \to \mathbb{C}$. Here's what I came up with: (Edit: this doesn't work; see Qiaochu's comment)

Define $P(x \in \mathbb{R}) = \frac{1}{1-e^{-x}}$ and $P^{-1}(x \in (0,1)) = -\log \frac{1-x}{x}$.

Let $f(x) = P(\operatorname{even}(P^{-1}(x))) \cdot e^{2 \pi i \operatorname{odd}(P^{-1}(x))}$, where

$$\operatorname{even}(0.b_0b_1b_2\ldots) = 0.b_0b_2b_4\ldots, \\\operatorname{odd}(0.b_0b_1b_2\ldots) = 0.b_1b_3b_5\ldots.$$

However, this seems needlessly complicated. Is there a simpler way?

-
$\text{even}$ and $\text{odd}$ are not well-defined functions (consider numbers that have more than one decimal expansion). You are much better off finding a bijection from $\mathbb{R}$ to a more manageable set first (I suggest $2^{\mathbb{N}}$). – Qiaochu Yuan Dec 2 '12 at 7:25
Even and odd are well-defined if you always use the finite expansion of numbers in $10^m\mathbb{Z}$ for $m\in \mathbb{Z}$. (The usual "greedy" expansion.) That the results of even and odd are not always of this form is no problem. In fact when you extend them to include digits in front of the decimal point you can use them as real and imaginary parts of your function. – WimC Dec 2 '12 at 8:37
@WimC: I'd consider your last sentence an easy-to-describe surjection. Can you post this as an answer? – Snowball Dec 2 '12 at 8:54
@Snowball That wouldn't add much to the existing answer and I did not describe what happens to negative numbers. It's not complete yet. – WimC Dec 2 '12 at 8:59

Basically, what you need to do is find a way to produce from one real number a pair of real numbers (to get the imaginary and real parts of the image). A standard trick to obtain two real numbers from one is as follows. First fix some convention that chooses between two decimal expansions (or any other base (except base 1)) in case a real number has two decimal expansions. Now, given a real number $t\in [0,1)$ with its given decimal expansion $0.d_1d_2\cdots$ define $x=0.d_1d_3d_5\cdots$ and $y=0.d_2d_4d_6\cdots$. This gives a bijection $[0,1)\to [0,1)\times [0,1)$ which you can extend in many ways to get a surjection (even a bijection) $\mathbb R\to \mathbb C$.

Later addition: As noted in the comments, the construction is not quite a bijection and the interval [0,1) should be changed to [0,1]. This can be corrected by noting that when the construction is restricted to irrationals the image misses only countably many elements. These then can be paired up with the rationals since they are also countable.

-
$(x, y)$ is not a bijection since $x$ and $y$ include both possible representations of numbers in $[0, 1)$ in their image. – WimC Dec 2 '12 at 8:57
agree by convention to always take representations ending with infinitely many 0's in case more than one representation exists. Then the construction is a bijection. – Ittay Weiss Dec 2 '12 at 9:06
To further @WinC 's comment: note that both $0.10191919\ldots$ and $0.11101010\ldots$ get sent to the pair $(0.1111\ldots , 0.1000\ldots = 0.0999\ldots )$. – arjafi Dec 2 '12 at 9:07
right (hitting myself in the head). I'll add that to the answer. – Ittay Weiss Dec 2 '12 at 9:11
It does not map to $[0, 1)^2$ and it is not a surjection to $[0,1]^2$ either since it misses $(1,1)$. – WimC Dec 2 '12 at 10:31

Since $f:\ {\mathbb R}\to{\mathbb R}^2$ is only required to be surjective you can even construct a continuous $f$ doing the trick:

Take your favorite Peano curve in the unit square, i.e., a continuous surjective function $\phi:\ [0,1]\to[0,1]^2$. Put $\phi(0)=: a$, $\phi(1)=:b$.

Number the integer squares $[j,j+1]\times[k,k+1]$ with even numbers $2\ell$ going from $-\infty$ to $\infty$, such that $[0,1]^2$ becomes $Q_0$, and you have two symmetrically outward going "spirals" of sequentially adjacent squares $Q_2$, $Q_4$, $Q_6$, $\ldots$, resp., $Q_{-2}$, $Q_{-4}$, $\ldots$, covering the whole plane. Denote the lower left corner of $Q_{2\ell}$ by $z_{2\ell}$.

Then for each $\ell\in{\mathbb Z}$ use the $t$-interval $[2\ell, 2\ell+1]$ to cover $Q_{2\ell}$ with a Peano curve by putting $$f(t):=z_{2\ell}+\phi(t-2\ell)\qquad(2\ell\leq t\leq 2\ell+1)\ .$$ Finally for each $\ell\in{\mathbb Z}$ use the $t$-interval $[2\ell+1, 2\ell+2]$ to connect the endpoint of the Peano curve in $Q_{2\ell}$ with the initial point of the Peano curve in $Q_{2(\ell +1)}$: $$f(t):=(2\ell+2-t)(z_{2\ell}+b)+(t-2\ell-1)(z_{2(\ell+1)}+a)\qquad(2\ell+1\leq t\leq 2\ell+2)\ .$$

-