ft_printf_xtoa.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ft_printf_xtoa.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: ll-hotel <ll-hotel@student.42.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/16 09:43:57 by ll-hotel #+# #+# */ 9 /* Updated: 2024/08/20 17:50:43 by ll-hotel ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "ft_basics.h" 14 #include "ft_math.h" 15 16 char *ft_xtoa(unsigned long value, short ox, short capital) 17 { 18 const char *const set = "0123456789abcdef"; 19 short i; 20 char *a; 21 22 i = ft_log_b(value, 16) + 1; 23 a = (char *)ft_calloc(i + 1 + (2 * ox), sizeof(char)); 24 if (!a) 25 return (0); 26 capital &= 1; 27 capital *= 32; 28 while (i) 29 { 30 a[(2 * ox) + --i] = set[value % 16] - capital * ((value % 16) > 9); 31 value /= 16; 32 } 33 if (ox) 34 { 35 a[0] = '0'; 36 a[1] = 'x' - capital; 37 } 38 return (a); 39 }