]> Creatis software - gdcm.git/commitdiff
ENH: Rewrite the base conversion from base 256 to base 10 using string operation...
authormalaterre <malaterre>
Fri, 28 Jan 2005 00:02:15 +0000 (00:02 +0000)
committermalaterre <malaterre>
Fri, 28 Jan 2005 00:02:15 +0000 (00:02 +0000)
ChangeLog
src/gdcmCommon.h
src/gdcmUtil.cxx

index 3fa355a12f0cb56887d47557b304e261b5f1b75f..83b400af79ad8d4392eb89449be4fad5b2742262 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2005-01-27 Mathieu Malaterre  <Mathieu.Malaterre@creatis.insa-lyon.fr>
+   * Rewrite the base 256 to base 10 conversion, using string operation only.
+        This allos us to get rid of the problematic 64bits integer type (does not
+        alwyas exist on target platform).
+
 2005-01-27 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
    * Add construct and destructor to class gdcm::Debug to close the debug file
           automatically
index ed76f8af7a111fc1cb77795cc114732704c3a281..16af192899687c59d0958fa4be1fba33a5785d91 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmCommon.h,v $
   Language:  C++
-  Date:      $Date: 2005/01/23 01:00:17 $
-  Version:   $Revision: 1.57 $
+  Date:      $Date: 2005/01/28 00:02:15 $
+  Version:   $Revision: 1.58 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 typedef  signed char         int8_t;
 typedef  signed short        int16_t;
 typedef  signed int          int32_t;
-typedef  signed __int64      int64_t;
 typedef  unsigned char       uint8_t;
 typedef  unsigned short      uint16_t;
 typedef  unsigned int        uint32_t;
-typedef  unsigned __int64    uint64_t;
 #define UINT32_MAX    (4294967295U)
 #endif
 
index d295c670f98bdae0437aad0bbd538d4afd213e9c..6880df0c9927683231d7d26a6237d137c85e0cd5 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/27 10:06:33 $
-  Version:   $Revision: 1.124 $
+  Date:      $Date: 2005/01/28 00:02:15 $
+  Version:   $Revision: 1.125 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -19,6 +19,7 @@
 #include "gdcmUtil.h"
 #include "gdcmDebug.h"
 #include <iostream>
+#include <algorithm>
 
 // For GetCurrentDate, GetCurrentTime
 #include <time.h>
@@ -665,6 +666,23 @@ int GetMacAddrSys ( unsigned char *addr )
 #endif //__sun
 }
 
+/**
+ * Mini function to return the last digit from a number express in base 256
+ * pre condition data contain an array of 6 unsigned char
+ * post condition carry contain the last digit
+ */
+inline int getlastdigit(unsigned char *data)
+{
+  int extended, carry = 0;
+  for(int i=0;i<6;i++)
+    {
+    extended = (carry << 8) + data[i];
+    data[i] = extended / 10;
+    carry = extended % 10;
+    }
+  return carry;
+}
+
 /**
  * \brief Encode the mac address on a fixed lenght string of 15 characters.
  * we save space this way.
@@ -677,24 +695,26 @@ std::string Util::GetMACAddress()
    // http://groups-beta.google.com/group/comp.unix.solaris/msg/ad36929d783d63be
    // http://bdn.borland.com/article/0,1410,26040,00.html
    unsigned char addr[6];
-   uint64_t n = 0;
  
    int stat = GetMacAddrSys(addr);
    if (stat == 0)
    {
-      // Horner evaluation
-      for(int i=0; i<6; i++)
-      {
-         n *= 256;
-         n += addr[i];
-      }
-
-      // we fit on 15 bytes maximum < 256^6.
-#if defined(_MSC_VER) || defined(__BORLANDC__)
-      return Format("%I64u", n);
-#else
-      return Format("%llu", n);
-#endif
+      // We need to convert a 6 digit number from base 256 to base 10, using integer
+      // would requires a 48bits one. To avoid this we have to reimplement the div + modulo 
+      // with string only
+      bool zero = false;
+      int res;
+      std::string sres;
+      while(!zero)
+        {
+        res = getlastdigit(addr);
+        sres.push_back( '0' + res );
+        zero = (addr[0] == 0) && (addr[1] == 0) && (addr[2] == 0) && (addr[3] == 0) && (addr[4] == 0) && (addr[5] == 0);
+        }
+      // Since we push back the proper number is reversed:
+      std::reverse(sres.begin(),sres.end());
+
+      return sres;
    }
    else
    {