tl;dr : How to convert an unsigned 32-bit integer to chars/uint8_t without random text
Okay, I am willing to sacrifice several reputation points for this. I need to quickly convert a 4-byte unsigned integer to array bytes in order to read/write/manipulate binary files of my own structure.
So that I can read a structure and then use it as an object, instead of reading it, writing in it for each an every change.
But when I try to implement a function I get a leakage. Pointer just keeps adding the values outside the scope of the function.
void trans_byte( uint32_t &A, uint8_t *out ){
    // i did this with loop too same thing happens
    uint8_t *ptr = (uint8_t*)&A;
    out[3] = *ptr;
    ptr++;
    out[2] = *ptr;
    ptr++;
    out[1] = *ptr;
    ptr++;
    out[0] = *ptr;
    ptr = ptr - 4;
}
uint32_t trans_ray( uint8_t *in ){
    return ( in[0] << 24 ) | ( in[1] << 16 ) | ( in[2] << 8 ) | in[3]; 
}
And in main :
int main(){
    std::cout << "SHUGA" << std::endl; // test printing to see if test works 
    uint32_t ADR = 0x6D4E4344; // memory should write magic of mNCD
    uint8_t ret[4];// array to be "outed" from function
    trans_byte(ADR, ret); // function
    uint8_t ret2[4] = { ret[0], ret[1], ret[2], ret[3] }; //copy of array to see if issue happens if i copy it
    std::cout << ret << std::endl; // printing the array 1
    std::cout << ret2 << std::endl; // printing the array 2
}
And output :
SHUGA
mNCDmNCD // keep in mind I called the function only once. 
mNCD
After I add to the main
std::cout << "shiya" << std::endl;
uint32_t ADR2 = trans_ray(ret);
std::cout << ret << std::endl;
std::cout << ret2 << std::endl;
std::cout << (ret==ret2) << std::endl;
And then I get this lovely mess:
SHUGA
mNCDmNCD─[uÉ@
mNCD─[uÉ@
shiya
mNCDmNCD─[uÉ@
mNCD─[uÉ@
0
So I am guessing that this is some sort of memory leak, and pointer keeps reading memory or something later on ( and therefore a plethora of symbols ).
So, how can I do this back and forth conversions from uint32_t to uint8_t without pointer continuing to read and manipulate an array even if it isn't called?
Before you start commenting:
I don't want to use std::vector since I don't want to add another 20kb or so of binary data per file to the existing large project be it file or PE.
I can't use std::string since I don't want to hunt for string ends \x00.
I tried using unions, but bytes are reversed, and for magic numbers and similar I need them to be in order. And even if I try to reverse the array I still need to run onto this issue.
I tried using struct-ures and class-es, but when I change something in object and I need to update the file, I still end up with this issue when writing code block.
 
     
     
    