'How to copy 64 bits to certain address? in C
I would like to copy 64 bits of variable message length and insert them at the last 64 bits of an array (uint8_t block[64]). I think that number 3 is stored in memory as 0000 ... 011 (64 bits total) so if I insert 64 bits of this variable into last 64 bits of uint8_t block[64], number 3 should be in block[63].
size_t length = 3;
uint64_t message_length = (uint64_t)length;
uint8_t block[64] = {0};
memcpy(&block[56], &message_length, 8);
Tried to achieve that with this code but unfortunately it doesn't work this way and number 3 is in block[56].
Solution 1:[1]
If it's an endianess issue as suggested by @harold comment, then the proper way to write the value in big endian would be:
size_t length = 3
uint64_t message_length = length
uint8_t block[64] = { 0 };
block[56] = message_length >> 56;
block[57] = message_length >> 48;
block[58] = message_length >> 40;
block[59] = message_length >> 32;
block[60] = message_length >> 24;
block[61] = message_length >> 16;
block[62] = message_length >> 8;
block[63] = message_length >> 0;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
