123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- using System;
- namespace NetMQ
- {
- /// <summary>
- /// This static class serves to convert between byte-arrays, and various integer sizes
- /// - all of which assume the byte-data is in Big-endian, or "Network Byte Order".
- /// </summary>
- public static class NetworkOrderBitsConverter
- {
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 16-bit integer derived from the 2 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the short from</param>
- /// <returns></returns>
- public static short ToInt16(byte[] buffer)
- {
- var i = buffer[0] << 8 |
- buffer[1];
- return (short)i;
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 16-bit integer derived from the 2 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the short from</param>
- /// <param name="offset">Offset to read from</param>
- /// <returns></returns>
- public static ushort ToUInt16(byte[] buffer, int offset)
- {
- var i = buffer[offset] << 8 |
- buffer[offset + 1];
- return (ushort)i;
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 16-bit integer derived from the 2 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the short from</param>
- /// <param name="offset"></param>
- /// <returns></returns>
- public static ushort ToUInt16(Span<byte> buffer, int offset)
- {
- var i = buffer[offset] << 8 |
- buffer[offset + 1];
- return (ushort)i;
- }
- /// <summary>
- /// Given a 16-bit integer, return it as a byte-array in Big-endian order.
- /// </summary>
- /// <param name="value">the short to convert</param>
- /// <returns>a 2-byte array containing that short's bits</returns>
- public static byte[] GetBytes(short value)
- {
- var buffer = new byte[2];
- PutInt16(value, buffer);
- return buffer;
- }
- /// <summary>
- /// Given a 16-bit integer, and a byte-array buffer and offset,
- /// - write the 2 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the short to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the short's bytes into</param>
- public static void PutInt16(short value, byte[] buffer)
- {
- buffer[0] = (byte)(value >> 8);
- buffer[1] = (byte) value;
- }
- /// <summary>
- /// Given a 16-bit integer, and a byte-array buffer and offset,
- /// - write the 2 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the short to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the short's bytes into</param>
- /// <param name="offset">Offset</param>
- public static void PutUInt16(ushort value, byte[] buffer, int offset = 0)
- {
- buffer[offset] = (byte)(value >> 8);
- buffer[offset + 1] = (byte) value;
- }
-
- /// <summary>
- /// Given a 16-bit integer, and a byte-array buffer and offset,
- /// - write the 2 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the short to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the short's bytes into</param>
- /// <param name="offset">Offset</param>
- public static void PutUInt16(ushort value, Span<byte> buffer, int offset = 0)
- {
- buffer[offset] = (byte)(value >> 8);
- buffer[offset + 1] = (byte) value;
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 32-bit integer derived from the 4 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the integer from</param>
- /// <param name="offset">offset</param>
- /// <returns></returns>
- public static int ToInt32(byte[] buffer, int offset = 0)
- {
- return
- buffer[offset] << 24 |
- buffer[offset + 1] << 16 |
- buffer[offset + 2] << 8 |
- buffer[offset + 3];
- }
-
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 32-bit integer derived from the 4 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the integer from</param>
- /// <returns></returns>
- public static int ToInt32(Span<byte> buffer)
- {
- return
- buffer[0] << 24 |
- buffer[1] << 16 |
- buffer[2] << 8 |
- buffer[3];
- }
- /// <summary>
- /// Given a 32-bit integer, return it as a byte-array in Big-endian order.
- /// </summary>
- /// <param name="value">the int to convert</param>
- /// <returns>a 4-byte array containing that integer's bits</returns>
- public static byte[] GetBytes(int value)
- {
- var buffer = new byte[4];
- PutInt32(value, buffer);
- return buffer;
- }
- /// <summary>
- /// Given a 32-bit integer, and a byte-array buffer and offset,
- /// - write the 4 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the integer to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the integer's bytes into</param>
- /// <param name="offset">Offset to write to</param>
- public static void PutInt32(int value, byte[] buffer, int offset = 0)
- {
- buffer[offset] = (byte)(value >> 24);
- buffer[offset + 1] = (byte)(value >> 16);
- buffer[offset + 2] = (byte)(value >> 8);
- buffer[offset + 3] = (byte) value;
- }
-
- /// <summary>
- /// Given a 32-bit integer, and a byte-array buffer and offset,
- /// - write the 4 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the integer to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the integer's bytes into</param>
- public static void PutInt32(int value, Span<byte> buffer)
- {
- buffer[0] = (byte)(value >> 24);
- buffer[1] = (byte)(value >> 16);
- buffer[2] = (byte)(value >> 8);
- buffer[3] = (byte) value;
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 64-bit integer derived from the 8 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the Int64 from</param>
- /// <returns></returns>
- public static long ToInt64(byte[] buffer)
- {
- return
- (long)buffer[0] << 56 |
- (long)buffer[1] << 48 |
- (long)buffer[2] << 40 |
- (long)buffer[3] << 32 |
- (long)buffer[4] << 24 |
- (long)buffer[5] << 16 |
- (long)buffer[6] << 8 |
- (long)buffer[7];
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 64-bit integer derived from the 8 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the Int64 from</param>
- /// <returns></returns>
- public static long ToInt64(Span<byte> buffer)
- {
- return
- (long)buffer[0] << 56 |
- (long)buffer[1] << 48 |
- (long)buffer[2] << 40 |
- (long)buffer[3] << 32 |
- (long)buffer[4] << 24 |
- (long)buffer[5] << 16 |
- (long)buffer[6] << 8 |
- (long)buffer[7];
- }
-
- /// <summary>
- /// Given a 64-bit integer, return it as a byte-array in Big-endian order.
- /// </summary>
- /// <param name="value">The <c>long</c> value to convert from.</param>
- /// <returns>The network order presentation of <paramref name="value"/> as an 8-byte array.</returns>
- public static byte[] GetBytes(long value)
- {
- var buffer = new byte[8];
- PutInt64(value, buffer);
- return buffer;
- }
- /// <summary>
- /// Given a 64-bit integer, and a byte-array buffer and offset,
- /// - write the 8 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the long value to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the long value's bytes into</param>
- public static void PutInt64(long value, byte[] buffer)
- {
- buffer[0] = (byte)(value >> 56);
- buffer[1] = (byte)(value >> 48);
- buffer[2] = (byte)(value >> 40);
- buffer[3] = (byte)(value >> 32);
- buffer[4] = (byte)(value >> 24);
- buffer[5] = (byte)(value >> 16);
- buffer[6] = (byte)(value >> 8);
- buffer[7] = (byte) value;
- }
- /// <summary>
- /// Given a 64-bit integer, and a byte-array buffer and offset,
- /// - write the 8 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the long value to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the long value's bytes into</param>
- public static void PutInt64(long value, Span<byte> buffer)
- {
- buffer[0] = (byte)(value >> 56);
- buffer[1] = (byte)(value >> 48);
- buffer[2] = (byte)(value >> 40);
- buffer[3] = (byte)(value >> 32);
- buffer[4] = (byte)(value >> 24);
- buffer[5] = (byte)(value >> 16);
- buffer[6] = (byte)(value >> 8);
- buffer[7] = (byte) value;
- }
-
- /// <summary>
- /// Given a 64-bit integer, and a byte-array buffer and offset,
- /// - write the 8 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the long value to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the long value's bytes into</param>
- public static void PutUInt64(ulong value, Span<byte> buffer)
- {
- buffer[0] = (byte)(value >> 56);
- buffer[1] = (byte)(value >> 48);
- buffer[2] = (byte)(value >> 40);
- buffer[3] = (byte)(value >> 32);
- buffer[4] = (byte)(value >> 24);
- buffer[5] = (byte)(value >> 16);
- buffer[6] = (byte)(value >> 8);
- buffer[7] = (byte) value;
- }
- /// <summary>
- /// Given a 64-bit integer, and a byte-array buffer and offset,
- /// - write the 8 bytes of that integer into the buffer starting at that offset, in Big-endian order.
- /// </summary>
- /// <param name="value">the long value to convert into bytes</param>
- /// <param name="buffer">the byte-array to write the long value's bytes into</param>
- /// <param name="offset">Offset to write to</param>
- public static void PutUInt64(ulong value, byte[] buffer, int offset)
- {
- buffer[offset] = (byte)(value >> 56);
- buffer[offset + 1] = (byte)(value >> 48);
- buffer[offset + 2] = (byte)(value >> 40);
- buffer[offset + 3] = (byte)(value >> 32);
- buffer[offset + 4] = (byte)(value >> 24);
- buffer[offset + 5] = (byte)(value >> 16);
- buffer[offset + 6] = (byte)(value >> 8);
- buffer[offset + 7] = (byte) value;
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 64-bit integer derived from the 8 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the Int64 from</param>
- /// <param name="offset">Offset to read from</param>
- /// <returns></returns>
- public static ulong ToUInt64(byte[] buffer, int offset)
- {
- return
- (ulong)buffer[offset] << 56 |
- (ulong)buffer[offset + 1] << 48 |
- (ulong)buffer[offset + 2] << 40 |
- (ulong)buffer[offset + 3] << 32 |
- (ulong)buffer[offset + 4] << 24 |
- (ulong)buffer[offset + 5] << 16 |
- (ulong)buffer[offset + 6] << 8 |
- (ulong)buffer[offset + 7];
- }
- /// <summary>
- /// Given a byte-array assumed to be in Big-endian order, and an offset into it
- /// - return a 64-bit integer derived from the 8 bytes starting at that offset.
- /// </summary>
- /// <param name="buffer">the byte-array to get the Int64 from</param>
- /// <param name="offset">Offset to read from</param>
- /// <returns></returns>
- public static ulong ToUInt64(Span<byte> buffer, int offset)
- {
- return
- (ulong)buffer[offset] << 56 |
- (ulong)buffer[offset + 1] << 48 |
- (ulong)buffer[offset + 2] << 40 |
- (ulong)buffer[offset + 3] << 32 |
- (ulong)buffer[offset + 4] << 24 |
- (ulong)buffer[offset + 5] << 16 |
- (ulong)buffer[offset + 6] << 8 |
- (ulong)buffer[offset + 7];
- }
- }
- }
|