B
Barzo
Hi,
I'm a quite newbie so...
I have a char* buffer = [1F][4D][BC][77][80] that represent a
timestamp with this convention:
// 00011111 01001101 10111100 01110111 10000000 -> 1F 4D BC 77
80
// | 2003 | 06 | 30 | 07 | 30 | 00 |
and I have to build a function that returns an std::string with value
"20030630073000" (YYYYMMDDhhmmss).
I have tought to do something like this:
std::string decode_time(const std::string& buffer)
{
double time;
std::stringstream ret_val;
time = (buffer[0] * 0x100000000) +
(buffer[1] * 0x1000000) +
(buffer[2] * 0x10000) +
(buffer[3] * 0x100) +
buffer[4];
unsigned short year = (((buffer[0] * 0x100) + buffer[1]) >> 2);
unsigned short month = ((((buffer[1] & 0x03) * 0x100) + (buffer[2]
& 0xC0) ) >> 6);
unsigned short day = (buffer[2] & 0x3E) >> 1;
unsigned short hour = ((((buffer[2] & 0x01) * 0x100) + (buffer[3]
& 0xF0) ) >> 4);
unsigned short minute = ((((buffer[3] & 0x0F) * 0x100) + (buffer[4]
& 0xC0) ) >> 6);
unsigned short second = (buffer[4] & 0x3F);
ret_val << year << month << day << hour << minute << second ;
return ret_val.str();
}
but I think there are more efficient ways..
Also..
- Could I substitute the time = (...) instruction with memcpy(&time,
buffer, 5) ?
- With this method I can't have months, days, hous always with 2
digits:
if I have month = 6 I have to encode it how "06"...
Tnx,
Daniele.
I'm a quite newbie so...
I have a char* buffer = [1F][4D][BC][77][80] that represent a
timestamp with this convention:
// 00011111 01001101 10111100 01110111 10000000 -> 1F 4D BC 77
80
// | 2003 | 06 | 30 | 07 | 30 | 00 |
and I have to build a function that returns an std::string with value
"20030630073000" (YYYYMMDDhhmmss).
I have tought to do something like this:
std::string decode_time(const std::string& buffer)
{
double time;
std::stringstream ret_val;
time = (buffer[0] * 0x100000000) +
(buffer[1] * 0x1000000) +
(buffer[2] * 0x10000) +
(buffer[3] * 0x100) +
buffer[4];
unsigned short year = (((buffer[0] * 0x100) + buffer[1]) >> 2);
unsigned short month = ((((buffer[1] & 0x03) * 0x100) + (buffer[2]
& 0xC0) ) >> 6);
unsigned short day = (buffer[2] & 0x3E) >> 1;
unsigned short hour = ((((buffer[2] & 0x01) * 0x100) + (buffer[3]
& 0xF0) ) >> 4);
unsigned short minute = ((((buffer[3] & 0x0F) * 0x100) + (buffer[4]
& 0xC0) ) >> 6);
unsigned short second = (buffer[4] & 0x3F);
ret_val << year << month << day << hour << minute << second ;
return ret_val.str();
}
but I think there are more efficient ways..
Also..
- Could I substitute the time = (...) instruction with memcpy(&time,
buffer, 5) ?
- With this method I can't have months, days, hous always with 2
digits:
if I have month = 6 I have to encode it how "06"...
Tnx,
Daniele.