I have the following codes:
#include "stdio.h"
#include "stdint.h"
#include <string>
#include "string.h"
#include<iostream>
using namespace std;
class myMessage
{
public:
uint8_t latitude[4]; //Binary
uint8_t longitude[4]; //Binary
};
int8_t sentMessage[4]={0,};
int main()
{
myMessage msg;
memset(&msg, 0x00, sizeof(msg));
uint8_t Input_Latitude[4] = {0x4,0x82,0x85,0xf1} ;
printf("%x ", Input_Latitude[0] );
printf("%x ", Input_Latitude[1] );
printf("%x ", Input_Latitude[2] );
printf("%x \n", Input_Latitude[3] );
memcpy(&msg.latitude[0], &Input_Latitude, sizeof(uint8_t) * 4);
printf("%x ", msg.latitude[0] );
printf("%x ", msg.latitude[1] );
printf("%x ", msg.latitude[2] );
printf("%x \n", msg.latitude[3] );
memcpy(&sentMessage[0], &msg.latitude[0], 4);
printf("%02X ", sentMessage[0] );
printf("%02X ", sentMessage[1] );
printf("%02X ", sentMessage[2] );
printf("%02X \n", sentMessage[3] );
}
When I print it out, I have: 4 82 85 f1 ==> original value
4 82 85 f1 ==> copy from unsign int to unsign int
04 FFFFFF82 FFFFFF85 FFFFFFF1 ==> after implicit converting from unsign int to int and printf
Why do we have a bunch of FFFFFFF ????
I understand that there is some implicit conversion from uint8 to int8 when I run
memcpy(&sentMessage[0], &msg.latitude[0], 4);
// sentMessage is int8_t
// msg.latitude[0] is uint8_t
However, I what I don't understand is some extra FFFFF that clearly exceeds the size of each element in array sentMessage[]