Timescale and bytes

I'm trying to insert a timestamp (hour: min: sec) into a 2 byte array and I'm a little confused on how to do this ... any help is greatly appreciated!

int Hour = CTime::GetCurrentTime().GetHour(); 
int Minute = CTime::GetCurrentTime().GetMinute(); 
int Second = CTime::GetCurrentTime().GetSecond(); 

BYTE arry[2];

//Need to insert 'Hour', 'Minute', & 'Second' into 'arry'

      

Thanks!

+2


a source to share


2 answers


You can not. There are potentially 86402 seconds per day (a day can have up to two leap seconds), but the 16 bits available to you in the array byte[2]

can only represent 65536 individual values.



+4


a source


  • hour: min: sec is not what people call a timestamp. The timestamp is the number of seconds that have passed since 1970-01-01 and will certainly not fit into 16 bits.
  • Assuming hour ranges = [0; 24], minutes = [0; 60], seconds = [0; 60] (including second seconds), you will need 5 + 6 + 6 = 17 bits, which still won't fit into 16 bits.

If you have a 32 bit array it will match:



int Hour = CTime::GetCurrentTime().GetHour(); 
int Minute = CTime::GetCurrentTime().GetMinute(); 
int Second = CTime::GetCurrentTime().GetSecond(); 

uint8_t array[4];

// Just an example
*(uint32_t*)array = (Hour << 12) | (Minute << 6) | Second;

      

Does this sound like homework to me ... or what exactly is it for?

+1


a source







All Articles