Function to convert little endian bytes into big endian I'm working on a little endian machine and in my program I need to convert 2,3,4,5 or 8 bytes into network order before transmitting them over the network. I've written the following function which basically just reverses the bytes. I just wanted to make sure if it's logic is correct.

 void convertLittleToBig(const uint8_t* in, uint8_t* out, const uint64_t&amp; sizeInBytes) {
      for(int i=0;i&lt;sizeInBytes;++i)
        out[i] = in[sizeInBytes-i-1];
    }


The function assumes that 'out' and 'in' will be pointing at 'sizeInBytes' memory.
If I'm not wrong, I can use the same function to convert from network order into little endian. Basically big endian and little endian, are mirror images of each other in terms of their byte order as per my current understanding.

Given below is a sample program:

#include &lt;iostream&gt;
#include &lt;cstdint&gt;
#include &lt;iostream&gt;

using std::cin;

//This function prints the raw bytes in memory.

void printBytes(const uint8_t* p, const uint64_t size) {
for(int i=0;i&lt;size;++i)
  printf("\nbyte[%d](%x)",i+1,p[i]);
printf("\n");
}

void convertLittleToBig(const uint8_t* in, uint8_t* out, const uint64_t&amp; sizeInBytes) {
  printf("\nCurrent representation\n");
  printBytes(in,sizeInBytes);
  for(int i=0;i&lt;sizeInBytes;++i)
    out[i] = in[sizeInBytes-i-1];
  printf("\nRepresentation after conversion\n");
  printBytes(out,sizeInBytes);
}

int main() {
  uint64_t little;
  printf("\nEnter a number that you wish to convert into big endian: ");
  int64_t inp;
  cin&gt;&gt;inp;
  little=inp;
  uint64_t big;
  convertLittleToBig((uint8_t*)&amp;little,(uint8_t*)&amp;big,sizeof(big));
}
Includes
We have included &lt;iostream&gt; twice, but missed &lt;cstdio&gt;.
Misspelt standard library identifiers
std::printf, std::uint8_t and std::uint64_t are consistently misspelt.  You might sometimes get away with this, as your standard library is allowed to add global-namespace versions of those identifiers; since it's not required to do so, you have a portability bug.
Use appropriate size type
std::size_t is the appropriate type to use for the size of an object, rather than std::uint64_t (which could be unnecessarily big, or - theoretically, at least for now - too small).
Conversion function shouldn't have side-effects
The printf() calls within convertLittleToBig make it unusable in any serious program.
Consider a standard algorithm
The &lt;algorithm&gt; header provides a very useful std::copy() function we can use if we have a reverse iterator to copy from or to.  We can get suitable iterators from std::span views onto the inputs (from C++20 onwards).
Avoid raw pointers
We could do better, using a template to accept values and return by value, inferring the size from the argument type:
#include &lt;algorithm&gt;
#include &lt;span&gt;

template&lt;typename T&gt;
T convertLittleToBig(const T&amp; val)
{
    auto in = std::as_bytes(std::span(&amp;val, 1));
    T result;
    auto out = std::as_writable_bytes(std::span(&amp;result, 1));
    std::copy(in.rbegin(), in.rend(), out.begin());
    return result;
}

That's much simpler - no counting needed, and the caller doesn't need to use sizeof (to work with odd-sized values, they need to be passed as arrays).
This is how we'd use it in a simple test program:
#include &lt;cstddef&gt;
#include &lt;cstdlib&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
    
std::string to_hex(auto const&amp; val)
{
    std::ostringstream oss;
    oss &lt;&lt; std::hex &lt;&lt; std::setfill('0');
    auto in = std::as_bytes(std::span(&amp;val, 1));
    for (auto c: in) {
        oss &lt;&lt; std::setw(2) &lt;&lt; std::to_integer&lt;unsigned int&gt;(c);
    }
    return oss.str();
}

// hex-print the value and its reversal
void demo_endian_swap(auto const&amp; val)
{
    std::cout &lt;&lt; to_hex(val) &lt;&lt; &quot; -&gt; &quot;
              &lt;&lt; to_hex(convertLittleToBig(val)) &lt;&lt; '\n';
}

#include &lt;array&gt;
int main() {
    // Demonstrate a selection of types
    demo_endian_swap(std::uint16_t{0x1234});
    demo_endian_swap(std::uint64_t{0x123456789abcde});
    demo_endian_swap(std::array&lt;unsigned char,5&gt;{1,2,3,4,5});
    //demo_endian_swap(&quot;abc&quot;);    // Invalid - can't return an array
    //demo_endian_swap(std::string{&quot;abc&quot;}); // oops - reverses whole structure
}

Example output:
3412 -&gt; 1234
debc9a7856341200 -&gt; 00123456789abcde
0102030405 -&gt; 0504030201