MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / toBytes

Function toBytes

libraries/printHelpers/printHelpers.cpp:281–335  ·  view source on GitHub ↗

official support to UDA == 1024^12 kilo mega giga tera peta exa (1024^6) zetta yotta xona weka vunda uda (1024^12) (treda Byte == TDB is the next one and it is 2 char so code wise difficult and as it is seldom used, support stops there. To have some support the code uses lowercase for the next 8 levels treda sorta rinta quexa pepta ocha nena minga luma (1024 ^21 ~~ 10^63)

Source from the content-addressed store, hash-verified

279// To have some support the code uses lowercase for the next 8 levels
280// treda sorta rinta quexa pepta ocha nena minga luma (1024 ^21 ~~ 10^63)
281char * toBytes(double value, uint8_t decimals)
282{
283 char * buffer = __printbuffer;
284 // to enable full range uncomment the following line
285 // char units[] = " KMGTPEZYXWVUtsrqponml";
286
287 char units[] = " KMGTPEZYXWVU";
288 uint8_t i = 0; // i is index of the unit array == powers of 1024.
289
290 if (isinf(value))
291 {
292 strcpy(buffer, "<inf>");
293 return buffer;
294 }
295
296 while(value >= 1024)
297 {
298 value /= 1024;
299 i++;
300 }
301 if (i == 0) decimals = 0;
302 if (decimals > 3) decimals = 3;
303
304 // WHOLE PART iv
305 int integerPart = value;
306 itoa(integerPart, &buffer[0], 10);
307
308 // DECIMALS
309 value -= integerPart;
310 uint8_t pos = strlen(buffer);
311 if (decimals > 0)
312 {
313 buffer[pos++] = '.';
314 while (decimals-- > 0)
315 {
316 value = value * 10;
317 buffer[pos++] = '0' + int(value);
318 value -= int(value);
319 }
320 }
321
322 // UNITS
323 if (i <= strlen(units))
324 {
325 if (i > 0) buffer[pos++] = ' ';
326 buffer[pos++] = units[i];
327 buffer[pos++] = 'B';
328 buffer[pos] = 0;
329 }
330 else
331 {
332 // no units available
333 }
334 return buffer;
335}
336
337
338////////////////////////////////////////////////////////////

Callers 1

unittestFunction · 0.85

Calls

no outgoing calls

Tested by 1

unittestFunction · 0.68