Convert infinite 2D plane integer coords to 1D number Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?
My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.
The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $u(x, y, z) = u(u(x, y), u(y, z))$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)
 A: Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:
$$ 123 $$
$$ 98765 $$
Add leading zeros to obtain equal number of digits:
$$ 00123 $$
$$ 98765 $$
And "interleave":
$$ 0908172635 $$
Reverting is trivial: you pick digits from either odd or even positions.
Notes:


*

*the representation depends on the base of the numeral system you use;

*you can expand the method to non-negative reals in a quite obvious way;

*similarly you can create a method that takes any fixed number of numbers and yields one number.

A: There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take $$f(n)=\left\{\begin{align}&2n&n\ge0\\&-2n-1&n<0\end{align}\right.$$ as Ross pointed out. Now us take the pair $(m,n)$ to be $2^{f(m)}3^{f(n)}$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.
A: This is an interesting question.  I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).
Let's begin with a point in the center.  That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite.  The point in the center is assigned the number 1, and we call it the $i = 1$ point.  We surround this square with a border of squares.  This border has 8 such squares.  We repeat the process and get 16 squares.  In general, each "border" has $2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$ squares. 
We now want to form a sum of the first $N$ such squares:
$$S_N = \bigg(\sum_{i=2}^{N}8(i-1)\bigg) + 1$$
Now, you want to simplify your life, so let's simplify that sum.  We would end up with:
$$S_N = 8\bigg(\sum_{i=2}^{N}(i-1)\bigg) + 1$$
$$S_N = 8\frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra.  I'm 99% sure it's accurate.
Now you want to unpack this.  This involves several steps.  Say you have the number $M$.  You need to find the largest $N$ such that $S_N \le M \lt S_{N+1}$.  Other than a strict search, I'm afraid I don't know how to do that, sorry.
Once you know the $N$, then you need to compute the quantity $M - S_N$.  This quantity tells you how many squares to "walk" from some starting square on border $N+1$ to where you want to be.  Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.
The method needs some cleanup, but should do it.  Good luck doing that in N-D.
A: You need the Cantor pairing function, tuned up to accept integers instead of naturals.  The basic function takes a pair of naturals (including zero) $x,y$ and returns a natural $\pi(x,y)=\frac 12(x+y)(x+y+1)+y$.  It is invertible, so given $\pi(x,y)$ you can recover $x$ and $y$. Now just take your integers to naturals by $$f(z)=\begin {cases} 2z&z\ge 0\\-2z-1& z \lt 0\end {cases}$$ pair them and you have your result.  
You can do your idea with the Ulam spiral:
 
The cell numbered $1$ is the origin.  Note the odd squares follow the downward right diagonal and the even squares start just above $1$ and follow an upward left diagonal.  If we are given a coordinate $(x,y)$ we first find the direction the side is going.  If $x \gt 0, x \gt |y|$, the cell is on an upward side.  The corner at the bottom is $(x,-x)$ and has number $(2x-1)^2$  The number in our cell is $y+x$ cells above, so the number is $(2x-1)^2+y+x$.  You can go through the other three sides similarly.  To go the other direction, given a cell number find the perfect square below it.  Find the location of the square on the diagonal, then count the number of squares from there as needed.
A: If you want to convert $(a,b)$ into $c$.
($a$,$b$,$c$ are all positive integer)
$$c = 2^{a-1} * (2*b-1)$$
This can uniquely map $(a,b)$ into $c$.
It is also invertible.
Moreover, $c$ is equal to all positive integers,
which means there are no waste of rooms(memories)
unlike the way, e.x. $$c = 2^{a-1} * 3^{b-1}$$
