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


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Sat Jun 8 2019 18:26:01