B
Barzo
Hi,
I'm sorry if my questions are banal...
I have to this 'simple' work:
INPUT: std::string buffer = [11][8E][CD][8D] --> <<CONVERSION>> -->
OUTPUT: std::string = "12,345345"
The buffer arrives from a device that use big-endian.
[11][8E][CD][8D] = 294571405
(294571405 * 90) / 0x80000000 = 12,34534496907....
My routine is (after have readed some posts here..) but it doen't
work:
template< typename T >
std::string ToString( const T& val )
{
try
{
return boost::lexical_cast<std::string>(val);
}
catch(const boost::bad_lexical_cast& e)
{
// std::cout << e.what();
return 0;
}
}
template<class T>
void fromBytes(unsigned char const* bytes, T* t)
{
*t = 0;
for(int i = 0; i != sizeof t; ++i)
*t |= bytes << 8 * i;
}
float decode_lat(const std::string& buffer, char token_pos)
{
// VAL = ((L * (2^31))/90) => L = ((VAL * 90)/(2^31))
// (2^31)=0x80000000
float lat = 0;
unsigned long val = 0;
// Extract the 4 latitude octets from the buffer and
// convert they into an long value
fromBytes<unsigned long>(reinterpret_cast<const unsigned char*>
(buffer.substr(token_pos, 4).c_str()), &val);
if (val == 0x7FFFFFFF)
lat = 90;
else
lat = ((unsigned long)(val * 90) / 0x80000000);
// The MSBit indicates the sign
return ( ((buffer[token_pos] & 0x80) == 0x80) ? -lat : lat );
}
void CallerFunction()
{...
std::string s = ToString<float>( decode_lat(buffer, token_pos) );
}
Could someone explain me what is wrong in this code?
Is this code portable? If not, is there a method to accomplish this?
Tnx in advance!
Daniele.
I'm sorry if my questions are banal...
I have to this 'simple' work:
INPUT: std::string buffer = [11][8E][CD][8D] --> <<CONVERSION>> -->
OUTPUT: std::string = "12,345345"
The buffer arrives from a device that use big-endian.
[11][8E][CD][8D] = 294571405
(294571405 * 90) / 0x80000000 = 12,34534496907....
My routine is (after have readed some posts here..) but it doen't
work:
template< typename T >
std::string ToString( const T& val )
{
try
{
return boost::lexical_cast<std::string>(val);
}
catch(const boost::bad_lexical_cast& e)
{
// std::cout << e.what();
return 0;
}
}
template<class T>
void fromBytes(unsigned char const* bytes, T* t)
{
*t = 0;
for(int i = 0; i != sizeof t; ++i)
*t |= bytes << 8 * i;
}
float decode_lat(const std::string& buffer, char token_pos)
{
// VAL = ((L * (2^31))/90) => L = ((VAL * 90)/(2^31))
// (2^31)=0x80000000
float lat = 0;
unsigned long val = 0;
// Extract the 4 latitude octets from the buffer and
// convert they into an long value
fromBytes<unsigned long>(reinterpret_cast<const unsigned char*>
(buffer.substr(token_pos, 4).c_str()), &val);
if (val == 0x7FFFFFFF)
lat = 90;
else
lat = ((unsigned long)(val * 90) / 0x80000000);
// The MSBit indicates the sign
return ( ((buffer[token_pos] & 0x80) == 0x80) ? -lat : lat );
}
void CallerFunction()
{...
std::string s = ToString<float>( decode_lat(buffer, token_pos) );
}
Could someone explain me what is wrong in this code?
Is this code portable? If not, is there a method to accomplish this?
Tnx in advance!
Daniele.