mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2025-12-16 14:07:56 +01:00
62 lines
1.4 KiB
C
62 lines
1.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_utils_convert.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/01/11 22:21:26 by fgras-ca #+# #+# */
|
|
/* Updated: 2024/01/11 22:23:53 by fgras-ca ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../cub3d.h"
|
|
|
|
static size_t ft_intlen(long n)
|
|
{
|
|
size_t len;
|
|
|
|
len = 0;
|
|
if (n == 0)
|
|
len++;
|
|
if (n < 0)
|
|
{
|
|
n *= -1;
|
|
len++;
|
|
}
|
|
while (n > 0)
|
|
{
|
|
n = n / 10;
|
|
len++;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
char *ft_itoa(int nb)
|
|
{
|
|
char *dst;
|
|
int i;
|
|
long n;
|
|
|
|
n = nb;
|
|
i = ft_intlen(n);
|
|
dst = (char *)malloc((i + 1) * sizeof(char));
|
|
if (!dst)
|
|
return (NULL);
|
|
dst[i--] = '\0';
|
|
if (n == 0)
|
|
dst[0] = '0';
|
|
if (n < 0)
|
|
{
|
|
dst[0] = '-';
|
|
n = n * -1;
|
|
}
|
|
while (n > 0)
|
|
{
|
|
dst[i] = '0' + (n % 10);
|
|
n = n / 10;
|
|
i--;
|
|
}
|
|
return (dst);
|
|
}
|