Beispiel #1
0
 public static void TestValidValuesFromBytes(Int32 value)
 {
     TestHelpers.CatchUnexpected(() => {
         byte[] buffer = Int24.GetBytes(value);
         int pos       = 0;
         Int24 val     = Int24.GetNew(buffer, ref pos);
         Assert.AreEqual(value, val.Value, string.Format("On Set with Int32:{0}", buffer.ToHexByteString()));
     });
 }
Beispiel #2
0
        public void Should_SerializeInt24()
        {
            Int24 value = Int24.MaxValue; // range -8,388,607 to 8,388,607

            Assert.AreEqual(false, value._sign);
            Assert.AreEqual(new byte[] { 255, 255, 127 }, value.GetBytes());
            Assert.AreEqual(new Bit[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, value.GetBits());

            value = Int24.MinValue;
            Assert.AreEqual(true, value._sign);
            Assert.AreEqual(new byte[] { 255, 255, 255 }, value.GetBytes());
            Assert.AreEqual(new Bit[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, value.GetBits());

            // test overflow
            value = (Int24)9000000;
            Assert.AreEqual(611392, value);
            Assert.AreEqual(false, value._sign);
            Assert.AreEqual(new byte[] { 64, 84, 9 }, value.GetBytes());
            Assert.AreEqual(new Bit[] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0 }, value.GetBits());
        }
Beispiel #3
0
        public override byte[] Write()
        {
            var buffer = new byte[256];
            var offset = 0;

            foreach (var _byte in BitConverter.GetBytes(Convert.ToInt16(this.RoleId)))
            {
                buffer[offset] = _byte;
                offset        += 1;
            }
            var RoleNameBytes = StringEncoding.GetBytes(this.RoleName);

            buffer[offset] = (byte)RoleNameBytes.Length;
            offset        += 1;
            foreach (var _byte in RoleNameBytes)
            {
                buffer[offset] = _byte;
                offset        += 1;
            }
            var IdBytes = Int24.GetBytes(this.Id);

            foreach (var _byte in IdBytes)
            {
                buffer[offset] = _byte;
                offset        += 1;
            }
            var countIds = this.Ids == null ? 0 : this.Ids.Count;

            buffer[offset] = (byte)countIds;
            offset++;
            if (countIds > 0)
            {
                foreach (var item in this.Ids)
                {
                    foreach (var _byte in  Int24.GetBytes(item))
                    {
                        buffer[offset] = _byte;
                        offset++;
                    }
                }
            }
            return(new ArraySegment <byte>(buffer, 0, offset).ToArray());
        }
Beispiel #4
0
 public void Write(Int24 value)
 {
     _writer.Write(value.GetBytes());
 }
Beispiel #5
0
 /// <summary>
 /// Copies the specified 24-bit signed integer value as an array of 3 bytes in the target endian-order to the destination array.
 /// </summary>
 /// <param name="value">The number to convert and copy.</param>
 /// <param name="destinationArray">The destination buffer.</param>
 /// <param name="destinationIndex">The byte offset into <paramref name="destinationArray"/>.</param>
 public void CopyBytes(Int24 value, byte[] destinationArray, int destinationIndex)
 {
     m_copyBuffer(Int24.GetBytes(value), 0, destinationArray, destinationIndex, 3);
 }
Beispiel #6
0
 /// <summary>
 /// Returns the specified 24-bit signed integer value as an array of bytes.
 /// </summary>
 /// <param name="value">The number to convert.</param>
 /// <returns>An array of bytes with length 3.</returns>
 public byte[] GetBytes(Int24 value)
 {
     return(m_coerceByteOrder(Int24.GetBytes(value)));
 }
Beispiel #7
0
 /// <summary>Performs proper bitwise conversion between signed and unsigned value</summary>
 /// <remarks>
 /// <para>This function is useful because CType(n, UInt24) will throw an OverflowException for values less than zero.</para>
 /// <para>For example, this function correctly converts signed 24-bit integer -8388608 (i.e., Int24.MinValue) to unsigned 24-bit integer 8388608.</para>
 /// </remarks>
 /// <param name="signedInt">Signed integer that is passed in to be converted to an unsigned integer.</param>
 /// <returns>The unsigned integer value.</returns>
 public static UInt24 ToUInt24(Int24 signedInt)
 {
     return(UInt24.GetValue(Int24.GetBytes(signedInt), 0));
 }
Beispiel #8
0
        /// <summary>
        /// Returns a simple encoded string representing a number which can later be decoded with <see cref="GetSeedFromKey"/>.
        /// </summary>
        /// <remarks>This function was designed for 24-bit values.</remarks>
        public static string GetKeyFromSeed(Int24 seed)
        {
            if (seed < 0)
                throw new ArgumentException("Cannot calculate key from negative seed");

            // This is a handy algorithm for encoding an integer value, use GetSeedFromKey to decode.
            byte[] seedBytes = seed.GetBytes();
            int alphaIndex;
            int asciiA = (int)'A';

            // Creates alpha-numeric key string.
            StringBuilder result = new StringBuilder();

            for (int x = 0; x < 3; x++)
            {
                alphaIndex = Random.Int32Between(0, 25);
                if (x > 0) result.Append('-');
                result.Append((char)(asciiA + (25 - alphaIndex)));
                result.Append(seedBytes[x] + alphaIndex);
            }

            return result.ToString();
        }