Number.cpp
Go to the documentation of this file.
1 
49 #include <cstdlib>
50 
51 using namespace std;
52 
53 namespace clearpath
54 {
55 
56  void utob(void *dest, size_t dest_len, uint64_t src)
57  {
58  size_t i;
59  /* Copy bytes from int to array */
60  for (i = 0; (i < dest_len) && (i < sizeof(uint64_t)); ++i)
61  {
62  ((uint8_t *) dest)[i] = (src >> (i * 8)) & 0xff;
63  }
64  /* If array is larger than int, 0-fill the remainder */
65  for (; i < dest_len; ++i)
66  {
67  ((uint8_t *) dest)[i] = 0;
68  }
69  }
70 
71  void itob(void *dest, size_t dest_len, int64_t src)
72  {
73  size_t i;
74  /* Copy bytes from int to array */
75  for (i = 0; (i < dest_len) && (i < sizeof(int64_t)); ++i)
76  {
77  ((uint8_t *) dest)[i] = (src >> (i * 8)) & 0xff;
78  }
79  /* If array is larger than int, sign-fill the remainder */
80  for (; i < dest_len; ++i)
81  {
82  if (((uint8_t *) dest)[dest_len - 1] & 0x80)
83  { // MSB is set, int is negative
84  ((uint8_t *) dest)[i] = 0xff;
85  }
86  else
87  { // int is positive
88  ((uint8_t *) dest)[i] = 0;
89  }
90  }
91  }
92 
93  void ftob(void *dest, size_t dest_len, double src, double scale)
94  {
95  int64_t int_src = (src * scale);
96  itob(dest, dest_len, int_src);
97  }
98 
99 /* Need to provide all these overloaded functions because integer promotion
100  * of smaller int types is ambiguous between the uint64/int64 */
101  void utob(void *dest, size_t dest_len, uint32_t src)
102  {
103  utob(dest, dest_len, (uint64_t) src);
104  }
105 
106  void utob(void *dest, size_t dest_len, uint16_t src)
107  {
108  utob(dest, dest_len, (uint64_t) src);
109  }
110 
111  void itob(void *dest, size_t dest_len, int32_t src)
112  {
113  itob(dest, dest_len, (int64_t) src);
114  }
115 
116  void itob(void *dest, size_t dest_len, int16_t src)
117  {
118  itob(dest, dest_len, (int64_t) src);
119  }
120 
121  uint64_t btou(void *src, size_t src_len)
122  {
123  uint64_t retval = 0;
124 
125  if (!src_len) { return 0; }
126  size_t i = src_len - 1;
127  do
128  {
129  retval = retval << 8;
130  retval |= ((uint8_t *) src)[i];
131  } while (i--);
132 
133  return retval;
134  }
135 
136  int64_t btoi(void *src, size_t src_len)
137  {
138  int64_t retval = 0;
139  size_t i = sizeof(int64_t);
140 
141  if (!src_len) { return 0; }
142 
143  /* If array is shorter than int, need to propagate sign bit */
144  for (; i >= src_len; --i)
145  {
146  retval = retval << 8;
147  if (((uint8_t *) src)[src_len - 1] & 0x80)
148  { // MSB is set, int is negative
149  retval |= 0xff;
150  }
151  }
152  do
153  {
154  retval = retval << 8;
155  retval |= ((uint8_t *) src)[i];
156  } while (i--);
157 
158  return retval;
159  }
160 
161  double btof(void *src, size_t src_len, double scale)
162  {
163  double retval = btoi(src, src_len);
164  return retval /= scale;
165  }
166 
167 }; // namespace clearpath
168 
void utob(void *dest, size_t dest_len, uint64_t src)
Definition: Number.cpp:56
int64_t btoi(void *src, size_t src_len)
Definition: Number.cpp:136
uint64_t btou(void *src, size_t src_len)
Definition: Number.cpp:121
void ftob(void *dest, size_t dest_len, double src, double scale)
Definition: Number.cpp:93
void itob(void *dest, size_t dest_len, int64_t src)
Definition: Number.cpp:71
double btof(void *src, size_t src_len, double scale)
Definition: Number.cpp:161


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Fri Oct 2 2020 03:40:07