WriteByte() public static method

Write a byte consisting of 1-8 bits to a buffer; assumes buffer is previously allocated
public static WriteByte ( byte source, int numberOfBits, byte destination, int destBitOffset ) : void
source byte
numberOfBits int
destination byte
destBitOffset int
return void
Ejemplo n.º 1
0
        public static void WriteUInt16(ushort source, int numberOfBits, byte[] destination, int destinationBitOffset)
        {
            if (numberOfBits == 0)
            {
                return;
            }

            NetException.Assert((numberOfBits >= 0 && numberOfBits <= 16), "numberOfBits must be between 0 and 16");
#if BIGENDIAN
            // reorder bytes
            uint intSource = source;
            intSource = ((intSource & 0x0000ff00) >> 8) | ((intSource & 0x000000ff) << 8);
            source    = (ushort)intSource;
#endif
            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
                return;
            }

            NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);

            numberOfBits -= 8;
            if (numberOfBits > 0)
            {
                NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset + 8);
            }
        }
 /// <summary>
 /// Writes 1 to 8 bits of a byte
 /// </summary>
 public void Write(byte source, int numberOfBits)
 {
     NetException.Assert((numberOfBits > 0 && numberOfBits <= 8), "Write(byte, numberOfBits) can only write between 1 and 8 bits");
     EnsureBufferSize(m_bitLength + numberOfBits);
     NetBitWriter.WriteByte(source, numberOfBits, m_data, m_bitLength);
     m_bitLength += numberOfBits;
 }
Ejemplo n.º 3
0
        public static int WriteUInt16(ushort source, int numberOfBits, byte[] destination, int destinationBitOffset)
        {
#if BIGENDIAN
            // reorder bytes
            uint intSource = source;
            intSource = ((intSource & 0x0000ff00) >> 8) | ((intSource & 0x000000ff) << 8);
            source    = (ushort)intSource;
#endif

            int returnValue = destinationBitOffset + numberOfBits;
            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }

            return(returnValue);
        }
 /// <summary>
 /// Writes a boolean value using 1 bit
 /// </summary>
 public NetBuffer Write(bool value)
 {
     EnsureBufferSize(m_bitLength + 1);
     NetBitWriter.WriteByte((value ? (byte)1 : (byte)0), 1, m_data, m_bitLength);
     m_bitLength += 1;
     return(this);
 }
 public NetBuffer Write(sbyte source)
 {
     EnsureBufferSize(m_bitLength + 8);
     NetBitWriter.WriteByte((byte)source, 8, m_data, m_bitLength);
     m_bitLength += 8;
     return(this);
 }
        /// <summary>
        /// Writes a byte at a given offset in the buffer
        /// </summary>
        public void WriteAt(Int32 offset, byte source)
        {
            int newBitLength = Math.Max(m_bitLength, offset + 8);

            EnsureBufferSize(newBitLength);
            NetBitWriter.WriteByte((byte)source, 8, m_data, offset);
            m_bitLength = newBitLength;
        }
Ejemplo n.º 7
0
        public static int WriteUInt32(uint source, int numberOfBits, byte[] destination, int destinationBitOffset)
        {
#if BIGENDIAN
            // reorder bytes
            source = ((source & 0xff000000) >> 24) |
                     ((source & 0x00ff0000) >> 8) |
                     ((source & 0x0000ff00) << 8) |
                     ((source & 0x000000ff) << 24);
#endif

            int returnValue = destinationBitOffset + numberOfBits;
            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            NetBitWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset);
            return(returnValue);
        }
 /// <summary>
 /// Writes a boolean value using 1 bit
 /// </summary>
 public void Write(bool value)
 {
     EnsureBufferSize(m_bitLength + 1);
     NetBitWriter.WriteByte((value ? (byte)1 : (byte)0), 1, m_data, m_bitLength);
     m_bitLength += 1;
 }
 public void Write(sbyte source)
 {
     EnsureBufferSize(m_bitLength + 8);
     NetBitWriter.WriteByte((byte)source, 8, m_data, m_bitLength);
     m_bitLength += 8;
 }
Ejemplo n.º 10
0
        public static int WriteUInt64(ulong source, int numberOfBits, byte[] destination, int destinationBitOffset)
        {
#if BIGENDIAN
            source = ((source & 0xff00000000000000L) >> 56) |
                     ((source & 0x00ff000000000000L) >> 40) |
                     ((source & 0x0000ff0000000000L) >> 24) |
                     ((source & 0x000000ff00000000L) >> 8) |
                     ((source & 0x00000000ff000000L) << 8) |
                     ((source & 0x0000000000ff0000L) << 24) |
                     ((source & 0x000000000000ff00L) << 40) |
                     ((source & 0x00000000000000ffL) << 56);
#endif

            int returnValue = destinationBitOffset + numberOfBits;
            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 24), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 32), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 32), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 40), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 40), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 48), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 48), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            if (numberOfBits <= 8)
            {
                NetBitWriter.WriteByte((byte)(source >> 56), numberOfBits, destination, destinationBitOffset);
                return(returnValue);
            }
            NetBitWriter.WriteByte((byte)(source >> 56), 8, destination, destinationBitOffset);
            destinationBitOffset += 8;
            numberOfBits         -= 8;

            return(returnValue);
        }
Ejemplo n.º 11
0
 //
 // 8 bit
 //
 internal void Write(byte source)
 {
     InternalEnsureBufferSize(m_bitLength + 8);
     NetBitWriter.WriteByte(source, 8, m_data, m_bitLength);
     m_bitLength += 8;
 }
 /// <summary>
 /// Write a <see cref="byte"/>.
 /// </summary>
 public static void Write(this IBitBuffer buffer, byte value)
 {
     buffer.EnsureEnoughBitCapacity(8);
     NetBitWriter.WriteByte(value, 8, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(8);
 }
 /// <summary>
 /// Writes a <see cref="bool"/> value using 1 bit.
 /// </summary>
 public static void WriteBit(this IBitBuffer buffer, bool value)
 {
     buffer.EnsureEnoughBitCapacity(1);
     NetBitWriter.WriteByte(value ? (byte)1 : (byte)0, 1, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(1);
 }
 /// <summary>
 /// Writes a <see cref="byte"/> using 1 to 8 bits.
 /// </summary>
 public static void Write(this IBitBuffer buffer, byte source, int bitCount)
 {
     buffer.EnsureEnoughBitCapacity(bitCount, maxBitCount: 8);
     NetBitWriter.WriteByte(source, bitCount, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(bitCount);
 }