本文整理匯總了C#中System.UInt32.LittleEndian方法的典型用法代碼示例。如果您正苦於以下問題:C# UInt32.LittleEndian方法的具體用法?C# UInt32.LittleEndian怎麽用?C# UInt32.LittleEndian使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.UInt32
的用法示例。
在下文中一共展示了UInt32.LittleEndian方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: VarInt
/// <summary>
/// Encoded an integer to save space.
/// </summary>
/// <remarks>
/// Integer can be encoded depending on the represented value to save space. Variable length integers always precede
/// an array/vector of a type of data that may vary in length. Longer numbers are encoded in little endian.
/// </remarks>
/// <specification>https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer</specification>
/// <example>
/// nodejs: https://c9.io/raistlinthewiz/bitcoin-coinbase-varint-nodejs
/// </example>
/// <returns></returns>
public static byte[] VarInt(UInt32 value)
{
if (value < 0xfd)
return new[] { (byte)value };
byte[] result;
using (var stream = new MemoryStream())
{
if (value < 0xffff)
{
stream.WriteValueU8(0xfd);
stream.WriteValueU16(((UInt16)value).LittleEndian());
}
else if (value < 0xffffffff)
{
stream.WriteValueU8(0xfe);
stream.WriteValueU32(value.LittleEndian());
}
else
{
stream.WriteValueU8(0xff);
stream.WriteValueU16(((UInt16)value).LittleEndian());
}
result = stream.ToArray();
}
return result;
}