unsigned long 到 char 数组
Unsigned long into char array
这是我的代码示例:
/* 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, '[=10=]' ,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, '[=10=]' ,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;
}
我的问题是,为什么最后一个 memcpy 有效?我希望将 CRYPTO 而不是 &CRYPTO 放在参数中......对我来说,CRYPTO 是我想要的值,所以 0xe8ba8fa3 和 &CRYPTO 地址。对我来说,CRYPTO 不是指针,所以为什么我需要将 memcpy 与 &CRYPTO 一起使用才能使其正常工作?
顺便说一句,我的代码可能是纯粹的灾难,我是初学者。不要犹豫,纠正我!
谢谢!
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.
你是对的。 CRYPTO
不是指针。但是,memcpy 需要一个指针,所以我们必须给它一个。我们通过获取 CRYPTO
的地址来做到这一点,这是通过向其添加 &
来完成的,因此 &CRYPTO
.
memcpy
所做的是将内存从一个地址复制到另一个地址(这就是它需要两个指针的原因),而不管这些地址的实际内容。如果你给它 CRYPTO
而不是指向它的指针,它可能会将 CRYPTO
的值解释为地址(行为未定义,除非编译器给出,否则无法保证会发生什么) .
memcpy void * memcpy ( void * destination, const void * source, size_t num );
参数
- destination :指向要复制内容的目标数组的指针,类型转换为 void* 类型的指针.
- source :指向要复制的数据源的指针,类型转换为 const void* 类型的指针。
- num :要复制的字节数。 size_t 是无符号整数类型。
这是我的代码示例:
/* 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, '[=10=]' ,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, '[=10=]' ,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;
}
我的问题是,为什么最后一个 memcpy 有效?我希望将 CRYPTO 而不是 &CRYPTO 放在参数中......对我来说,CRYPTO 是我想要的值,所以 0xe8ba8fa3 和 &CRYPTO 地址。对我来说,CRYPTO 不是指针,所以为什么我需要将 memcpy 与 &CRYPTO 一起使用才能使其正常工作?
顺便说一句,我的代码可能是纯粹的灾难,我是初学者。不要犹豫,纠正我!
谢谢!
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.
你是对的。 CRYPTO
不是指针。但是,memcpy 需要一个指针,所以我们必须给它一个。我们通过获取 CRYPTO
的地址来做到这一点,这是通过向其添加 &
来完成的,因此 &CRYPTO
.
memcpy
所做的是将内存从一个地址复制到另一个地址(这就是它需要两个指针的原因),而不管这些地址的实际内容。如果你给它 CRYPTO
而不是指向它的指针,它可能会将 CRYPTO
的值解释为地址(行为未定义,除非编译器给出,否则无法保证会发生什么) .
memcpy void * memcpy ( void * destination, const void * source, size_t num );
参数
- destination :指向要复制内容的目标数组的指针,类型转换为 void* 类型的指针.
- source :指向要复制的数据源的指针,类型转换为 const void* 类型的指针。
- num :要复制的字节数。 size_t 是无符号整数类型。