Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned long into char array

Here is an example of my code :

/* Standard Linux headers */


/* --------------------------------------------------------------------------
Calculates the CRYPTO 
-------------------------------------------------------------------------- */
unsigned long CalculateCRYPTO(
unsigned long ulCount, /* Number of bytes in the data block */
unsigned char *ucBuffer )  /*Data block*/
{
    unsigned long ulCRYPTO = 0;
    //fonction that i have not coded ...
    return( ulCRYPTO );

}


int main (void)
{

  /*Variables and socket programming*/

   //this is my datablock must be in hexa AA 35 07 (will chnage size and data but for now it's hardcoded)
    unsigned char datablock[3];
    memset(datablock, '\0' ,sizeof(datablock));
    datablock[0]=0xaa;
    datablock[1]=0x35;
    datablock[2]=0x07;

    unsigned long CRYPTO;
    CRYPTO=CalculateCRYPTO(sizeof(datablock),datablock); //calculate crypto depending of datablocks
    printf("CRYPTO = 0x%08x \n", CRYPTO); //prints me 0xe8ba8fa3 that is what i want 

    /*Creating the final command*/
    //(will chnage size and data but for now it's fixed)
    unsigned char cmd_final_hex[7]; //this needs to be DATABLOCKS+CRYPTO
                                    //in hexa AA 35 07 concatenated to inverted CRYPTO so ... A3 8F BA E8 => must be AA 35 07 A3 8F BA E8
    memset(cmd_final_hex, '\0' ,sizeof(cmd_final_hex));     //Make sure cmd final is at 0
    memcpy(cmd_final_hex, datablock, sizeof(datablock));    //cmd at datablock + 000
    // while loop prints me what i want so cmd_final_hex[]=AA 35 07 00 00 ...

    //Now i want to concatenate so my idea is to use memcpy and not strcat :
    memcpy(&cmd_final_hex[sizeof(datablock)], &CRYPTO, 4);

   //and a print gives me AA 35 07 A3 8F BA E8 which is exactly what i want but why do i have to use "&CRYPTO" and not "CRYPTO" in my memcpy. !!!

  return 0;


}

My question is, why does this last memcpy works ? i would expect to put CRYPTO and not &CRYPTO in arguments... For me, CRYPTO is the value i want so 0xe8ba8fa3 and &CRYPTO the address. And for me, CRYPTO is not a pointer, so why i need to use memcpy with &CRYPTO to make it works ?

By the way, my code may be pure disaster, i'm a beginner. Do not hesitate to correct me !

Thank you !

like image 516
HacHac Avatar asked Mar 05 '23 01:03

HacHac


1 Answers

My question is, why does this last memcpy works ? i would expect to put CRYPTO and not &CRYPTO in arguments... For me, CRYPTO is the value i want so 0xe8ba8fa3 and &CRYPTO the address.

You're right. CRYPTO is not a pointer. However, memcpy expects a pointer, so we have to give it one. We do this by taking CRYPTO 's address, and this is done by adding & to it, hence &CRYPTO.

What memcpy does is copying the memory from one address to the other address (that's why it takes two pointers), regardless of the actual content at those addresses. If you gave it CRYPTO instead of a pointer to it, it would likely interpret the value of CRYPTO as an address (the behavior is undefined, there's no guarantee for what will happen unless the compiler gives one).

like image 192
Blaze Avatar answered Mar 10 '23 10:03

Blaze