public byte[] ConvertTo(object value) { TypeSerializer.CheckArgument <decimal>(value); var decimalValue = (decimal)value; int[] bits = decimal.GetBits(decimalValue); int scale = (bits[3] >> 16) & 31; byte[] scaleBytes = BeConverter.GetBytes(scale); var bigintBytes = new byte[13]; // 13th byte is for making sure that the number is positive Buffer.BlockCopy(bits, 0, bigintBytes, 0, 12); var bigInteger = new BigInteger(bigintBytes); if (decimalValue < 0) { bigInteger = -bigInteger; } bigintBytes = bigInteger.ToByteArray(); Array.Reverse(bigintBytes); var resultBytes = new byte[scaleBytes.Length + bigintBytes.Length]; Array.Copy(scaleBytes, resultBytes, scaleBytes.Length); Array.Copy(bigintBytes, 0, resultBytes, scaleBytes.Length, bigintBytes.Length); return(resultBytes); }
/// <summary> /// Writes the body length in the frame and returns the frame length /// </summary> public int Close() { //Set the length in the header //MemoryStream implementation length and offset are ints, so cast is safe var frameLength = Convert.ToInt32(_stream.Length - _offset); var lengthBytes = BeConverter.GetBytes(frameLength - FrameHeader.GetSize(_version)); //The length could start at the 4th or 5th position long lengthOffset = 4; if (_version.Uses2BytesStreamIds()) { lengthOffset = 5; } //Set the position of the stream where the frame body length should be written _stream.Position = _offset + lengthOffset; _stream.Write(lengthBytes, 0, lengthBytes.Length); _stream.Position = _stream.Length; return(frameLength); }
/// <summary> /// Writes the frame header, leaving body length to 0 /// </summary> public void WriteFrameHeader(byte flags, short streamId, byte opCode) { byte[] header; if (!_version.Uses2BytesStreamIds()) { //8 bytes for the header, dedicating 1 for the streamId if (streamId > 127) { throw new ArgumentException("StreamId must be smaller than 128 under protocol version " + _version); } header = new byte[] { (byte)_version, flags, (byte)streamId, opCode, //Reserved for the body length 0, 0, 0, 0 }; Write(header); return; } //9 bytes for the header, dedicating 2 for the streamId var streamIdBytes = BeConverter.GetBytes(streamId); header = new byte[] { (byte)_version, flags, streamIdBytes[0], streamIdBytes[1], opCode, //Reserved for the body length 0, 0, 0, 0 }; Write(header); }
/// <summary> /// Writes Big Endian long /// </summary> public void WriteLong(long value) { Write(BeConverter.GetBytes(value)); }
/// <summary> /// Writes BE int /// </summary> public void WriteInt32(int value) { Write(BeConverter.GetBytes(value)); }
/// <summary> /// Writes BE int 16 /// </summary> public void WriteInt16(short value) { Write(BeConverter.GetBytes(value)); }