Writing char buffer in integer variables using c++. How to avoid writing the same piece of code for every variable?

I am trying to convert a char buffer into multiple int variables. This is what i got so far after a day of trying.

ZeroMemory(buf, 4096);
            int bytesReceived = recv(sock, buf, 4096, 0);
            if (bytesReceived > 0)
            {
                /*for (int i = 0; i < bytesReceived; i++)
                {
                    cout << hex << (int)buf(i);
                }*/
                std::stringstream ss;
                std::stringstream aa;
                for (int i = 0; i < 2; i++)
                    ss << std::hex << (int)buf(i);

                unsigned result;
                ss >> result;
                int Number1 = result;
                cout << Number1 << "n";

                for (int j = 2; j < 4; j++)
                    aa << std::hex << (int)buf(j);

                unsigned result2;
                aa >> result2;
                int Number2 = result2;
                cout << Number2 << "n";
            }

This gives me in the console the desired number 20000 and 10000. Now how do i do this without writing for every time i want to do this, this whole part:

for (int j = 2; j < 4; j++)
                    aa << std::hex << (int)buf(j);

                unsigned result2;
                aa >> result2;
                int Number2 = result2;
                cout << Number2 << "n";

My brain is now fried after getting to whis point from zero knowledge. Would appreciate it if someone could help me out.