Number.cpp
Go to the documentation of this file.
00001 
00048 #include "roch_base/core/Number.h"
00049 #include <cstdlib>
00050 using namespace std;
00051 
00052 namespace sawyer
00053 {
00054 
00055   void utob(void *dest, size_t dest_len, uint64_t src)
00056   {
00057     size_t i;
00058     /* Copy bytes from int to array */
00059     for (i = 0; (i < dest_len) && (i < sizeof(uint64_t)); ++i)
00060     {
00061       ((uint8_t *) dest)[i] = (src >> (i * 8)) & 0xff;
00062     }
00063     /* If array is larger than int, 0-fill the remainder */
00064     for (; i < dest_len; ++i)
00065     {
00066       ((uint8_t *) dest)[i] = 0;
00067     }
00068   }
00069 
00070   void itob(void *dest, size_t dest_len, int64_t src)
00071   {
00072     size_t i;
00073     /* Copy bytes from int to array */
00074     for (i = 0; (i < dest_len) && (i < sizeof(int64_t)); ++i)
00075     {
00076       ((uint8_t *) dest)[i] = (src >> (i * 8)) & 0xff;
00077     }
00078     /* If array is larger than int, sign-fill the remainder */
00079     for (; i < dest_len; ++i)
00080     {
00081       if (((uint8_t *) dest)[dest_len - 1] & 0x80)
00082       { // MSB is set, int is negative
00083         ((uint8_t *) dest)[i] = 0xff;
00084       }
00085       else
00086       { // int is positive
00087         ((uint8_t *) dest)[i] = 0;
00088       }
00089     }
00090   }
00091 
00092   void ftob(void *dest, size_t dest_len, double src, double scale)
00093   {
00094     int64_t int_src = (src * scale);
00095     itob(dest, dest_len, int_src);
00096   }
00097 
00098 /* Need to provide all these overloaded functions because integer promotion
00099  * of smaller int types is ambiguous between the uint64/int64 */
00100   void utob(void *dest, size_t dest_len, uint32_t src)
00101   {
00102     utob(dest, dest_len, (uint64_t) src);
00103   }
00104 
00105   void utob(void *dest, size_t dest_len, uint16_t src)
00106   {
00107     utob(dest, dest_len, (uint64_t) src);
00108   }
00109 
00110   void itob(void *dest, size_t dest_len, int32_t src)
00111   {
00112     itob(dest, dest_len, (int64_t) src);
00113   }
00114 
00115   void itob(void *dest, size_t dest_len, int16_t src)
00116   {
00117     itob(dest, dest_len, (int64_t) src);
00118   }
00119 
00120   uint64_t btou(void *src, size_t src_len)
00121   {
00122     uint64_t retval = 0;
00123 
00124     if (!src_len) { return 0; }
00125     size_t i = src_len - 1;
00126     do
00127     {
00128       retval = retval << 8;
00129       retval |= ((uint8_t *) src)[i];
00130     } while (i--);
00131 
00132     return retval;
00133   }
00134 
00135   int64_t btoi(void *src, size_t src_len)
00136   {
00137     int64_t retval = 0;
00138     size_t i = sizeof(int64_t);
00139 
00140     if (!src_len) { return 0; }
00141 
00142     /* If array is shorter than int, need to propagate sign bit */
00143     for (; i >= src_len; --i)
00144     {
00145       retval = retval << 8;
00146       if (((uint8_t *) src)[src_len - 1] & 0x80)
00147       {  // MSB is set, int is negative
00148         retval |= 0xff;
00149       }
00150     }
00151     do
00152     {
00153       retval = retval << 8;
00154       retval |= ((uint8_t *) src)[i];
00155     } while (i--);
00156 
00157     return retval;
00158   }
00159 
00160   double btof(void *src, size_t src_len, double scale)
00161   {
00162     double retval = btoi(src, src_len);
00163     return retval /= scale;
00164   }
00165 
00166 }; // namespace sawyer
00167 


roch_base
Author(s): Mike Purvis , Paul Bovbel , Carl
autogenerated on Sat Jun 8 2019 20:32:33