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


roch_base
Author(s): Mike Purvis , Paul Bovbel , Chen
autogenerated on Mon Jun 10 2019 14:41:14