dprintf_atoi32.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* dprintf_atoi32.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: ll-hotel <ll-hotel@student.42lyon.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2024/02/08 14:19:10 by ll-hotel #+# #+# */ 9 /* Updated: 2024/03/17 21:34:16 by ll-hotel ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "dprintf_inner.h" 14 15 void ft_atoi32(char *out, int32_t n, const char *abase, uint8_t base) 16 { 17 const uint16_t log = ft_logb(n, base); 18 const uint8_t negative = n < 0; 19 int32_t tmp; 20 uint16_t i; 21 22 tmp = (n + (n == (int32_t)0x80000000)); 23 tmp *= !negative - negative; 24 i = log + negative; 25 out[0] = '-'; 26 out[i] = abase[tmp % base]; 27 i += (n != 0); 28 while (i > negative) 29 { 30 out[--i] = abase[tmp % base]; 31 tmp = tmp / base; 32 } 33 if (n == (int32_t)0x80000000) 34 out[10] += 1; 35 } 36 37 void ft_atoui32(char *out, uint32_t n, const char *abase, uint8_t base) 38 { 39 uint16_t i; 40 41 i = ft_logbu(n, base); 42 out[i] = abase[n % base]; 43 i += (i > 0); 44 while (i > 0) 45 { 46 out[--i] = abase[n % base]; 47 n /= base; 48 } 49 }