MQTT-Gateway/test/strtoint.c
2024-01-24 22:08:06 +01:00

45 lines
774 B
C

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int *strtoint(char *in, size_t start, size_t n)
{
int *ret = malloc(sizeof(int));
*ret = 0;
int offset = start + n - 1;
if (offset < 0)
{
return NULL;
}
for (int i = 0; i < n; i++) {
int position = offset - i;
printf("foo: %c\n", in[position]);
if (in[position] >= '0' && in[position] <= '9')
{
*ret += (in[position] - '0') * pow(10, i);
}
else
{
return NULL;
}
}
return ret;
}
int main()
{
char *in = "abcd123";
int *ret = strtoint(in, 4, 3);
if (ret != NULL)
{
printf("%s | %d\n", in, *ret);
}
else
{
printf("ret is NULL\n");
}
}