/// <summary> /// Writes a signed tag to the stream /// </summary> /// <param name="value">The value to write</param> /// <param name="tag">The tag number, or 255 for an application tag</param> public void WriteSigned64(long value, byte tag = 255) { int length; if (value < (1L << 7) && value >= (-1L << 7)) { length = 1; } else if (value < (1L << 15) && value >= (-1L << 15)) { length = 2; } else if (value < (1L << 23) && value >= (-1L << 23)) { length = 3; } else if (value < (1L << 31) && value >= (-1L << 31)) { length = 4; } else if (value < (1L << 39) && value >= (-1L << 39)) { length = 5; } else if (value < (1L << 47) && value >= (-1L << 47)) { length = 6; } else if (value < (1L << 55) && value >= (-1L << 55)) { length = 7; } else { length = 8; } _writeLengthHeader(tag, ApplicationTag.Signed, length); var bytes = new Union8(value).ReverseLE(); switch (length) { case 8: _writer.Write(bytes.Byte0); goto case 7; case 7: _writer.Write(bytes.Byte1); goto case 6; case 6: _writer.Write(bytes.Byte2); goto case 5; case 5: _writer.Write(bytes.Byte3); goto case 4; case 4: _writer.Write(bytes.Byte4); goto case 3; case 3: _writer.Write(bytes.Byte5); goto case 2; case 2: _writer.Write(bytes.Byte6); goto case 1; case 1: _writer.Write(bytes.Byte7); break; } }
/// <summary> /// Writes an unsigned tag to the stream /// </summary> /// <param name="value">The value to write</param> /// <param name="tag">The tag number, or 255 for an application tag</param> public void WriteUnsigned64(ulong value, byte tag = 255) { int length; if (value < (1ul << 8)) { length = 1; } else if (value < (1ul << 16)) { length = 2; } else if (value < (1ul << 24)) { length = 3; } else if (value < (1ul << 32)) { length = 4; } else if (value < (1ul << 40)) { length = 5; } else if (value < (1ul << 48)) { length = 6; } else if (value < (1ul << 56)) { length = 7; } else { length = 8; } _writeLengthHeader(tag, ApplicationTag.Unsigned, length); value = new Union8(value << (64 - (length * 8))).Reverse().UInt64; for (int i = 0; i < length; i++) { _writer.Write((byte)value); value >>= 8; } }
/// <summary> /// Writes an unsigned tag to the stream /// </summary> /// <param name="value">The value to write</param> /// <param name="tag">The tag number, or 255 for an application tag</param> public void WriteUnsigned64(ulong value, byte tag = 255) { int length; if (value < (1ul << 8)) length = 1; else if (value < (1ul << 16)) length = 2; else if (value < (1ul << 24)) length = 3; else if (value < (1ul << 32)) length = 4; else if (value < (1ul << 40)) length = 5; else if (value < (1ul << 48)) length = 6; else if (value < (1ul << 56)) length = 7; else length = 8; _writeLengthHeader(tag, ApplicationTag.Unsigned, length); value = new Union8(value << (64 - (length * 8))).Reverse().UInt64; for(int i = 0; i < length; i++) { _writer.Write((byte)value); value >>= 8; } }
/// <summary> /// Writes a signed tag to the stream /// </summary> /// <param name="value">The value to write</param> /// <param name="tag">The tag number, or 255 for an application tag</param> public void WriteSigned64(long value, byte tag = 255) { int length; if (value < (1L << 7) && value >= (-1L << 7)) length = 1; else if (value < (1L << 15) && value >= (-1L << 15)) length = 2; else if (value < (1L << 23) && value >= (-1L << 23)) length = 3; else if (value < (1L << 31) && value >= (-1L << 31)) length = 4; else if (value < (1L << 39) && value >= (-1L << 39)) length = 5; else if (value < (1L << 47) && value >= (-1L << 47)) length = 6; else if (value < (1L << 55) && value >= (-1L << 55)) length = 7; else length = 8; _writeLengthHeader(tag, ApplicationTag.Signed, length); var bytes = new Union8(value).ReverseLE(); switch (length) { case 8: _writer.Write(bytes.Byte0); goto case 7; case 7: _writer.Write(bytes.Byte1); goto case 6; case 6: _writer.Write(bytes.Byte2); goto case 5; case 5: _writer.Write(bytes.Byte3); goto case 4; case 4: _writer.Write(bytes.Byte4); goto case 3; case 3: _writer.Write(bytes.Byte5); goto case 2; case 2: _writer.Write(bytes.Byte6); goto case 1; case 1: _writer.Write(bytes.Byte7); break; } }