You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.1 KiB

/*
* helpers.c
*
* Created on: Dec 7, 2017
* Author: maximilian
*/
//--------------Includes-----------------
#include"stm32f10x.h"
#include"helpers.h"
#include"error.h"
#include<stdlib.h>
uint8_t osItoa(int iint, char* iochar, size_t ibuffsize, size_t* obuffsize)
{
int i = 0;
int j = 0;
char z[16];
int isnegative = 0;
size_t buff_size_1 = ibuffsize;
size_t buff_size_2 = sizeof(z);
/* Signed or unsigned? */
if(iint < 0)
{
iint *= -1;
isnegative = 1;
}
if(iint == 0)
{
z[i] = '0';
i++;
}
/* Take last digit and convert until iint is gone. */
while(iint > 0)
{
z[i] = (char)('0' + iint % 10);
iint /= 10;
i++;
if( ((buff_size_1 - 1) < (unsigned int)i) || ((buff_size_2 - 1) < (unsigned int)i))
{
THROW_ERROR(E_BUFFER_OVERFLOW);
return 0;
}
}
/* Fix the minus symbol. */
if( ((buff_size_1 - 1) < (unsigned int)i))
return 0;
else if(isnegative)
{
z[i] = '-';
i++;
}
/* Copy into output (reverse it as well while doing so). */
j = i-1;
while(j >= 0)
{
iochar[j] = z[i-j-1];
j--;
}
if(obuffsize != NULL)
*obuffsize = i;
return 1;
}