2016-07-11 5 views
2

tl; dr boost :: streambufから作成されたistreamから浮動小数点を読み取ることができません。私が持っているデータは、壊れているか、間違って処理されています。boost :: asio UDPソケットからistreamを使用してfloatを取得する

私は現在、DIS(Distributed Interactive Simulation)プロトコルを実装するインターフェイスを作成しています。私は、UDPソケットを開き、データを受け取るためにboost :: asioライブラリを使用しています。私はデータをワイヤーからstreambufに正しく引き出していますが、フロートを読み取ることはできません。ソケットの

コード:

boost::asio::streambuf streamBuf; 
boost::asio::streambuf::mutable_buffers_type buf = streamBuf.prepare(maxSize); 

udp::endpoint senderEndpoint; 
size_t packetLength; 

try { 
    packetLength = sock.receive_from(buf, senderEndpoint); 
    streamBuf.commit(packetLength); 
} catch(boost::system::system_error se) { 
    std::cout << "ERROR: " << se.what() << std::endl; 
} 
std::istream stream(&streamBuf); 

コードはistreamから

float  readFloat32(std::istream& stream) { 
    float ret; 
    stream.read((char*)&ret, sizeof(float)); 
    return ret; 
} 
double  readFloat64(std::istream& stream) { 
    double ret; 
    stream.read((char*)&ret, sizeof(double)); 
    return ret; 
} 

を読むために今、私は私が必要とするすべてのデータ型(主にuint8_t - のuint32_t)のために書かれた同様の機能を持っています。 PDU

//--Entity ID 
    entityState.entityID.siteID = readUInt16(stream); 
    entityState.entityID.appID = readUInt16(stream); 
    entityState.entityID.entityID = readUInt16(stream); 
...etc, etc. 
//--Entity Linear Velocity 
    entityState.entityLinearVelocity.x = readFloat32(stream); 
    entityState.entityLinearVelocity.y = readFloat32(stream); 
    entityState.entityLinearVelocity.z = readFloat32(stream); 
...etc, etc. 

データを解析

私は非常に最後の写真のようなルックスを解析しようとしています。私はすべてうまく解析することができますが、3つの32ビット浮動小数点数からなる「Entity Linear Velocity」ブロックに到達すると、データが間違っていきます。 wiresharkを使用して、適切なbtyestreamが配線を介して送信されていることを確認しました。

 42 74 aa 97 c2 80 b3 8f 42 e2 dc 54

61.166592、-64.350700と113.430328に相当:私はバイトのこのシリーズを送っていた速度のために

。しかし、私のアプリケーションは、これを読み取る

42 72 ffffffaa ffffff97 ffffffc2 ffffff80 ffffffb3 ffffff8f 42 ffffffe2 ffffffdc 54

そして、私のフロートは-1.10153e-24、-1.77004e-29、および7.58951e + 12になります。

私はこの実装を使用していろいろな種類のさまざまなサイズのウイントを読み込むことができますが、フロートは意味をなさないのですか?

enter image description here

答えて

0

おそらく問題をエンディアンによるものです。試験した場合、次のコードの種類がbyteswapping即ちntohl

std::string bin = {'\x42', '\x74', '\xaa', '\x97'}; 
std::istringstream raw(bin); 

float readFloat32(std::istream& stream) { 
    union { 
     float fval; 
     uint32_t ival; 
    } ret; 
    stream.read((char*)&ret, sizeof(float)); 
    ret.ival = ntohl(ret.ival); 
    return ret.fval; 
} 

と協力readFloatによって返される値はbyteswappingを加えた後61.1666ました。

関連する問題