$\DeclareMathOperator\SL{SL}\DeclareMathOperator\Aut{Aut}$Fix $d < 0$, a fundamental quadratic discriminant and $n$ a positive integer. Suppose $Q$ is a primitive binary quadratic form of discriminant $d$. Let us define the following,


1. Representations of $n$ by $Q$: $R(Q, n) = \{ (x, y) \in \mathbb{Z}^2 | Q(x, y) = n\}$
2. $\Aut(Q)$ as the subgroup of $\SL\_2(\mathbb{Z})$ that fixes $Q$ under the usual action of $\SL\_2(\mathbb{Z})$ on binary quadratic forms.
3. $R(d, n) = \coprod\_QR(Q, n)/\Aut(Q)$ as $Q$ runs through a complete set of representatives for each class of properly equivalent forms. Typically, we may select the reduced forms.
4. $I(d, n) = \{I \textrm{ proper ideals of the ring of integers of } K \textrm{ of norm } n \}$ where $K = \mathbb{Q}[\sqrt{(d)}]$ the quadratic numberfield of discriminant $d$.
5. The Epstein zeta function for $Q$: $\zeta(s, Q) = \frac{1}{2} \sum\_{(x, y) \neq (0, 0)}{(Q(x, y))}^{-s}$ for $\mathfrak{R}(s) > 1$ for $(x, y) \in \mathbb{Z}^2$
6. The Dirichlet zeta function for $K$: $\zeta(s, K) = \sum\_{I}{Nm(I)}^{-s}$ for $\mathfrak{R}(s) > 1$ as $I$ runs through all the proper integer ideals of $K$.


As a fact I know that when $d < -4$, $\sum\_{[Q]}\zeta(s, Q) = \zeta(s, K)$. Also in I read that in general $|R(d, n)| = \sum\_{m|n}{\chi\_{d}(m)} = |I(d, n)|$ where $\chi\_{d}$ is the Kronecker symbol mod $|d|$. All these facts tend to point out that there is a one-one correspondence between $R(d,n)$ and $I(d, n)$.
My question is that whether such a correspondence exists? If so could you explain this correspondence? Does this hold for $d > 0$ also?



Correspondence between binary quadratic representations and proper ideals of quadratic number fields
As explained below, there is a correspondence between non-zero locally principal ideals $I$ in the order of disc. $D$ and quadratic forms $Q$ of disc. $D$. The form $Q$ is naturally defined on $I$ via $x \in I \mapsto N(x)/N(I).$ So $Q(x) = n$ iff $N(x) = n N(I)$ if $N(x I^{-1}) = n,$ so representations of $n$ by $Q$ correspond to ideals in the class
of $I^{-1}$ having norm $n$.


If we sum over all ideal classes, or equivalently over all $Q$, (and divide by the number of automorphisms of $Q$, which is the group of units in the order, to count ideals rather than elements that generate them) we will get the number of ideals of norm $n$.


============


The discovery of this correspondence must go back to the 19th century, maybe to Dirichlet or Dedekind? And Will Jagy's answer gives a very concrete description of it.


One can also give a more conceptual description of it. Indeed,
it is fairly easy, and classical, to describe the quadratic form attached to an ideal class in conceptual terms; I explain this at the end of my answer. There is also a conceptual approach to going from quadratic forms to ideal classes, but as far as I know it is more recent, and due to Melanie Wood (a version of it is mentioned in [this answer](https://mathoverflow.net/a/52815/169863)).


Namely, if $Q(x,y)$ is a quadratic form over $\mathbb Z$ which is primitive, then one can consider $\mathrm{Proj} \mathbb Z[x,y]/Q(x,y)$, which turns out to be finite flat of degree $2$ over $\mathbb Z$, therefore affine, of the form $\mathrm{Spec} A$ for some quadratic order $A$. And in fact $A$ is isomorphic to $R[(D +\sqrt{D})/2]$, where $D$ is the discriminant of $Q$ (so $A$ is the quadratic order of discriminant $D$). (There is no canonical identification of $A$ with this order though; we can compose with the automorphism $\sqrt{D} \mapsto -\sqrt{D}$ to get
another isomorphism.)


Now $\mathrm{Proj}$ comes with a canonical line bundle $\mathcal O(1)$, and so this is an element of $\mathrm{Pic} A$, which can be thought of as the class group
of non-zero locally principal ideals in $A$. (The usual class group when $D$ is a fundamental discriminant.)


Using the isomorphism $A \cong R[(D +\sqrt{D})/2],$ we get an element of the
class group of $R[(D +\sqrt{D})/2],$ or really a pair $I,I^{-1}$ of elements,
because applying the automorphism $\sqrt{D} \mapsto -\sqrt{D}$ switches $I$
and it's inverse.


This gives a map  

$$\text{ primitive quadratic forms } Q \text{ of discriminant } D
\longrightarrow \text{ pairs } I, I^{-1} \text{ in the class group of }
R[(D +\sqrt{D})/2].$$


To see it is a bijection, one can give an explicit inverse (as I mentioned above,
this is more classical):


If $I$ is a locally principal ideal in $R[(D +\sqrt{D})/2],$ then the formula
$$x \in I \mapsto N(x)/ N(I) \in \mathbb{Z}$$
defines a quadratic form $Q$ on the free rank two $\mathbb{Z}$-module $I$. (Note that $I^{-1}$ will give the same quadratic form, since formation of norms is invariant under conjugation.)


