本文整理汇总了C#中SerializationContext.EncodeInt32方法的典型用法代码示例。如果您正苦于以下问题:C# SerializationContext.EncodeInt32方法的具体用法?C# SerializationContext.EncodeInt32怎么用?C# SerializationContext.EncodeInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerializationContext
的用法示例。
在下文中一共展示了SerializationContext.EncodeInt32方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeDecimal
public static int SerializeDecimal(decimal value, SerializationContext context, bool lengthPrefixed)
{
int[] bits = decimal.GetBits(value);
ulong a = ((ulong)bits[1]) << 32, b = ((ulong)bits[0]) & 0xFFFFFFFFL;
ulong low = a | b;
uint high = (uint) bits[2];
uint signScale = (uint)(((bits[3] >> 15) & 0x01FE) | ((bits[3] >> 31) & 0x0001));
int len = 0;
if (lengthPrefixed)
{
if (low != 0) len += 1 + SerializationContext.GetLength((long)low);
if (high != 0) len += 1 + SerializationContext.GetLength((long)high);
if (signScale!=0) len += 2;
len = context.EncodeInt32(len);
}
if (low != 0)
{
context.WriteByte((FieldDecimalLow << 3) | (int)WireType.Variant);
len += 1 + context.EncodeInt64((long)low);
}
if (high != 0)
{ // note encode as long to avoid high sign issues
context.WriteByte((FieldDecimalHigh << 3) | (int)WireType.Variant);
len += 1 + context.EncodeInt64((long)high);
}
if (signScale != 0)
{
context.WriteByte((FieldDecimalSignScale << 3) | (int)WireType.Variant);
context.WriteByte((byte)signScale);
len += 2;
}
return len;
}
示例2: SerializeTimeSpan
internal static int SerializeTimeSpan(TimeSpan timeSpan, SerializationContext context, bool lengthPrefixed)
{
TimeSpanScale scale;
long value = timeSpan.Ticks;
if (timeSpan == TimeSpan.MaxValue)
{
value = 1;
scale = TimeSpanScale.MinMax;
}
else if (timeSpan == TimeSpan.MinValue)
{
value = -1;
scale = TimeSpanScale.MinMax;
}
else if (value % TimeSpan.TicksPerDay == 0)
{
scale = TimeSpanScale.Days;
value /= TimeSpan.TicksPerDay;
}
else if (value % TimeSpan.TicksPerHour == 0)
{
scale = TimeSpanScale.Hours;
value /= TimeSpan.TicksPerHour;
}
else if (value % TimeSpan.TicksPerMinute == 0)
{
scale = TimeSpanScale.Minutes;
value /= TimeSpan.TicksPerMinute;
}
else if (value % TimeSpan.TicksPerSecond == 0)
{
scale = TimeSpanScale.Seconds;
value /= TimeSpan.TicksPerSecond;
}
else if (value % TimeSpan.TicksPerMillisecond == 0)
{
scale = TimeSpanScale.Milliseconds;
value /= TimeSpan.TicksPerMillisecond;
}
else
{
scale = TimeSpanScale.Ticks;
}
int len = 0;
ulong zig = SerializationContext.Zig(value);
if (lengthPrefixed)
{
if (scale != TimeSpanScale.Days)
{
len += 2;
}
if (zig != 0)
{
len += 1 + SerializationContext.GetLength(zig);
}
context.WriteByte((byte)len);
len = 1;
}
if (value != 0)
{
context.WriteByte((FieldTimeSpanValue << 3) | (int)WireType.Variant);
len += 1 + context.EncodeUInt64(zig);
}
if (scale != TimeSpanScale.Days)
{
context.WriteByte((FieldTimeSpanScale << 3) | (int)WireType.Variant);
len += 1 + context.EncodeInt32((int)scale);
}
return len;
}