シリアライズしたい場合は、protobuf-net を使用して 10 進数をデシリアライズします。
const decimal originalDecimal = 1.6641007661819458m;
using (var memoryStream = new MemoryStream())
{
Serializer.Serialize(memoryStream, originalDecimal);
memoryStream.Position = 0;
var deserializedDecimal = Serializer.Deserialize<decimal>(memoryStream);
Assert.AreEqual(originalDecimal, deserializedDecimal);
}
それは正常に動作します。Protobuf-net は内部的に以下の小数表現を使用します (cf. Bcl.proto ):
message Decimal {
optional uint64 lo = 1; // the first 64 bits of the underlying value
optional uint32 hi = 2; // the last 32 bis of the underlying value
optional sint32 signScale = 3; // the number of decimal digits, and the sign
}
ここで、同等と思われるプロト コントラクトをコードで定義するとします。
[ProtoContract]
public class MyDecimal
{
[ProtoMember(1, IsRequired = false)]
public ulong Lo;
[ProtoMember(2, IsRequired = false)]
public uint Hi;
[ProtoMember(3, IsRequired = false)]
public int SignScale;
}
...その後、aをシリアライズしてバックを取得することも、a をシリアライズしてバックdecimal
を取得することもできません。MyDecimal
MyDecimal
decimal
から: decimal
_MyDecimal
const decimal originalDecimal = 1.6641007661819458m;
using (var memoryStream = new MemoryStream())
{
Serializer.Serialize(memoryStream, originalDecimal);
memoryStream.Position = 0;
// following line throws a Invalid wire-type ProtoException
Serializer.Deserialize<MyDecimal>(memoryStream);
}
から: MyDecimal
_decimal
var myDecimal = new MyDecimal
{
Lo = 0x003b1ee886632642,
Hi = 0x00000000,
SignScale = 0x00000020,
};
using (var memoryStream = new MemoryStream())
{
Serializer.Serialize(memoryStream, myDecimal);
memoryStream.Position = 0;
// following line throws a Invalid wire-type ProtoException
Serializer.Deserialize<decimal>(memoryStream);
}
ここで何か不足していますか?
私は、プロトコル バッファーを介して C# アプリケーションと通信する必要がある C++ アプリケーションに取り組んでおり、10 進数の逆シリアル化が失敗する理由を理解できません。