]> granicus.if.org Git - taglib/commitdiff
Add a toInt() method that correctly adjusts for the signedness bit even on
authorScott Wheeler <wheeler@kde.org>
Mon, 26 Apr 2004 23:00:21 +0000 (23:00 +0000)
committerScott Wheeler <wheeler@kde.org>
Mon, 26 Apr 2004 23:00:21 +0000 (23:00 +0000)
different variable sized byte vectors.

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@306690 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

toolkit/tbytevector.cpp
toolkit/tbytevector.h

index 5a27c60c1f7836cf9d747d9c83bd02f68779aa1d..59d1648ed5b399195266a596b03861a22efce5c5 100644 (file)
@@ -483,6 +483,26 @@ TagLib::uint ByteVector::toUInt(bool mostSignificantByteFirst) const
   return sum;
 }
 
+int ByteVector::toInt(bool mostSignificantByteFirst) const
+{
+  int sum = 0;
+  int last = d->data.size() > 4 ? 3 : d->data.size() - 1;
+
+  bool negative = uchar(d->data[mostSignificantByteFirst ? 0 : last]) & 0x80;
+
+  if(negative) {
+    for(int i = 0; i <= last; i++)
+      sum |= uchar(d->data[i] ^ 0xff) << ((mostSignificantByteFirst ? last - i : i) * 8);
+    sum = (sum + 1) * -1;
+  }
+  else {
+    for(int i = 0; i <= last; i++)
+      sum |= uchar(d->data[i]) << ((mostSignificantByteFirst ? last - i : i) * 8);
+  }
+
+  return sum;
+}
+
 long long ByteVector::toLongLong(bool mostSignificantByteFirst) const
 {
   // Just do all of the bit operations on the unsigned value and use an implicit
index 8a7b8cfd5c0279127e7649fbb3ecccf5a20276f7..afe3c162db52815e6a89aa77aadcf35798147551 100644 (file)
@@ -243,6 +243,21 @@ namespace TagLib {
      */
     uint toUInt(bool mostSignificantByteFirst = true) const;
 
+    /*!
+     * Converts the first 4 bytes of the vector to an integer.
+     *
+     * If \a mostSignificantByteFirst is true this will operate left to right
+     * evaluating the integer.  For example if \a mostSignificantByteFirst is
+     * true then $00 $00 $00 $01 == 0x00000001 == 1, if false, $01 00 00 00 ==
+     * 0x01000000 == 1.
+     *
+     * This will correctly adjust for the bit indicated signedness being the most
+     * significant bit of the most significant byte.
+     *
+     * \see fromUInt()
+     */
+    int toInt(bool mostSignificantByteFirst = true) const;
+
 
     /*!
      * Converts the first 8 bytes of the vector to a (signed) long long.