C++ (Qt)#pragma pack (push, 4)struct CTest { char rebyat1[10]; // первый урод (10 байт) char rebyat2[10]; // второй union { int rebyatenok_I; float rebyatenok_F; };};#pragma pack (pop) CTest test;ba.read(&test, sizeof(test));printf("int = %d, float = %f\n", test.rebyatenok_I, test.rebyatenok_F);
C++ (Qt)#include <QtGui/QApplication> #include <QtCore/QTextCodec>#include <QtCore/QDebug> #if __BYTE_ORDER == __LITTLE_ENDIAN #define MAN1OFFSET 0#define MAN2OFFSET 4#define EXP_OFFSET 6#define lecpy memcpy #elif __BYTE_ORDER == __BIG_ENDIAN #define MAN1OFFSET 4#define MAN2OFFSET 0#define EXP_OFFSET 0 static inline void *lecpy(char *dst, const char *src, size_t size) { dst += size; while (size--) *--dst = *src++; return dst;} #endif /* __BYTE_ORDER */ void fp80le_to_fp64(void *f64, const void *f80) { uint32_t man1, man2; uint16_t exp; lecpy((char *) &man1, (const char *) f80, 4); lecpy((char *) &man2, (const char *) f80 + 4, 4); lecpy((char *) &exp, (const char *) f80 + 8, 2); man1 >>= 11; man1 |= (man2 & 0x7ff) << 21; man2 >>= 11; man2 &= 0x000fffff; man2 |= (uint32_t) (exp & 0x8000) << 16; exp &= 0x7fff; exp = (uint16_t)(exp - 0x3c00); man2 |= (uint32_t) (exp & 0x7ff) << 20; memcpy((char *) f64 + MAN1OFFSET, &man1, 4); memcpy((char *) f64 + MAN2OFFSET, &man2, 4);} void fp64_to_fp80le(void *f80, const void *f64) { uint32_t man1, man2; uint16_t exp; memcpy(&man1, (const char *) f64 + MAN1OFFSET, 4); memcpy(&man2, (const char *) f64 + MAN2OFFSET, 4); memcpy(&exp, (const char *) f64 + EXP_OFFSET, 2); man2 = ((man2 << 11) & 0x7ffff800) | ((man1 >> 21) & 0x000007ff); if (man1 || man2) man2 |= 0x80000000; man1 <<= 11; if (man1 & 0x800) man1 |= 0x7ff; exp = (uint16_t)(((((exp >> 4) & 0x7ff) + 0x3c00) & 0x7fff) | (exp & 0x8000)); lecpy((char *) f80, (const char *) &man1, 4); lecpy((char *) f80 + 4, (const char *) &man2, 4); lecpy((char *) f80 + 8, (const char *) &exp, 2);} int main(int argc, char *argv[]){ QApplication a(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("CP1251")); QByteArray decodeString = QByteArray::fromBase64("uB6F61G4npAEQASSsG8nERHeBECDKkQA"); QByteArray ba = decodeString.mid(0,10); QByteArray bb = decodeString.mid(10,10); QByteArray bc = decodeString.mid(20,4); double *ld; double *lw; fp80le_to_fp64(ld, ba.data()); fp80le_to_fp64(lw, bb.data()); qDebug() << "size=" << decodeString.count(); qDebug() << "1 - double=" << *ld; qDebug() << "2 - double=" << *lw; qDebug() << "3 - unknown=" << bc.data(); char a1[10]; char a2[10]; fp64_to_fp80le(a1, ld); fp64_to_fp80le(a2, lw); QByteArray arr; arr.append(a1); arr.append(a2); arr.append(bc.data()); arr = arr.toBase64(); qDebug() << arr.data(); return 0;}
C++ (Qt)#include <QtCore/QDebug> bool getPositionFromBase64( const QString &string, long double &longitude, long double &latitude, int &altitude ){ QByteArray data = QByteArray::fromBase64( string.toAscii() ); if( data.size() != 24 ) { return false; } longitude = 0; latitude = 0; memcpy( &longitude, data.data(), 10 ); memcpy( &latitude, data.data() + 10, 10 ); memcpy( &altitude, data.data() + 20, 4 ); return true;} QString getBase64FromPosition( const long double &longitude, const long double &latitude, const int &altitude ){ char data[24] = { 0 }; memcpy( data, &longitude, 10 ); memcpy( data + 10, &latitude, 10 ); memcpy( data + 20, &altitude, 4 ); return QString::fromAscii( QByteArray( data, 24 ).toBase64() );} int main(int argc, char *argv[]){ long double longitude; long double latitude; int altitude; if( getPositionFromBase64( "uB6F61G4npAEQASSsG8nERHeBECDKkQA", longitude, latitude, altitude ) ) { qDebug() << "Longitude:" << (double)longitude; qDebug() << "Latitude:" << (double)latitude; qDebug() << "Altitude:" << altitude; qDebug() << "Generate Base64:" << getBase64FromPosition( longitude, latitude, altitude ); } else { qDebug() << "Wrong string"; } return 0;}