B
Barzo
Hi,
in a my previous post (http://groups.google.it/group/comp.lang.c++/
browse_frm/thread/fcd414d039dbd6be#) I had to convert a buffer into a
timestamp string:
[1F][4D][BC][77][80] -> 20030630073000
Now I have to do the opposite: having a string 20030630073000 I have
to return a buffer [1F][4D][BC][77][80].
The econding is like this:
00011111 01001101 10111100 01110111 10000000 -> 1F 4D BC 77 80
| 2003 | 06 | 30 | 07 | 30 | 00 |
where the year use 14 bits, months 4, days 5 and so on..
I have tought to do something like this:
int encode_time(const std::string& value, std::string& ret_val)
{
std::stringstream ss;
int i;
double d;
int st;
//value has to be YYYYMMDDhhmmss
ss << value.substr(0, 4);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d = i * 0x4000000;
ss.flush();
ss << value.substr(4, 2);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d += i * 0x400000;
ss.flush();
ss << value.substr(6, 2);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d += i * 0x20000;
ss.flush();
.....
.....
ss.flush();
ss << d;
ret_val = ss.str();
}
The first conversion (0, 4) is ok and i=2003, but the second one (4,2)
fails and the func returns 7.
Where I wrong?
Tnx.
Daniele.
in a my previous post (http://groups.google.it/group/comp.lang.c++/
browse_frm/thread/fcd414d039dbd6be#) I had to convert a buffer into a
timestamp string:
[1F][4D][BC][77][80] -> 20030630073000
Now I have to do the opposite: having a string 20030630073000 I have
to return a buffer [1F][4D][BC][77][80].
The econding is like this:
00011111 01001101 10111100 01110111 10000000 -> 1F 4D BC 77 80
| 2003 | 06 | 30 | 07 | 30 | 00 |
where the year use 14 bits, months 4, days 5 and so on..
I have tought to do something like this:
int encode_time(const std::string& value, std::string& ret_val)
{
std::stringstream ss;
int i;
double d;
int st;
//value has to be YYYYMMDDhhmmss
ss << value.substr(0, 4);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d = i * 0x4000000;
ss.flush();
ss << value.substr(4, 2);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d += i * 0x400000;
ss.flush();
ss << value.substr(6, 2);
if( (ss >> i).fail() ) { st = ss.rdstate(); return st;}
d += i * 0x20000;
ss.flush();
.....
.....
ss.flush();
ss << d;
ret_val = ss.str();
}
The first conversion (0, 4) is ok and i=2003, but the second one (4,2)
fails and the func returns 7.
Where I wrong?
Tnx.
Daniele.