return sum;
}
+
+ template <class T>
+ ByteVector fromNumber(T value, bool mostSignificantByteFirst)
+ {
+ int size = sizeof(T);
+
+ ByteVector v(size, 0);
+
+ for(int i = 0; i < size; i++)
+ v[i] = uchar(value >> ((mostSignificantByteFirst ? size - 1 - i : i) * 8) & 0xff);
+
+ return v;
+ }
}
using namespace TagLib;
ByteVector ByteVector::fromUInt(uint value, bool mostSignificantByteFirst)
{
- ByteVector v(4, 0);
-
- for(int i = 0; i < 4; i++)
- v[i] = uchar(value >> ((mostSignificantByteFirst ? 3 - i : i) * 8) & 0xff);
+ return fromNumber<uint>(value, mostSignificantByteFirst);
+}
- return v;
+ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst)
+{
+ return fromNumber<short>(value, mostSignificantByteFirst);
}
ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst)
{
- ByteVector v(8, 0);
-
- for(int i = 0; i < 8; i++)
- v[i] = uchar(value >> ((mostSignificantByteFirst ? 7 - i : i) * 8) & 0xff);
-
- return v;
+ return fromNumber<long long>(value, mostSignificantByteFirst);
}
////////////////////////////////////////////////////////////////////////////////
*/
static ByteVector fromUInt(uint value, bool mostSignificantByteFirst = true);
+ /*!
+ * Creates a 2 byte ByteVector based on \a value. If
+ * \a mostSignificantByteFirst is true, then this will operate left to right
+ * in building the ByteVector. For example if \a mostSignificantByteFirst is
+ * true then $00 01 == 0x0001 == 1, if false, $01 00 == 0x0100 == 1.
+ *
+ * \see toShort()
+ */
+ static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
+
/*!
* Creates a 8 byte ByteVector based on \a value. If
* \a mostSignificantByteFirst is true, then this will operate left to right