Skip to content

Commit

Permalink
print_hex
Browse files Browse the repository at this point in the history
  • Loading branch information
JennyHadir committed Oct 30, 2020
1 parent 886220f commit b9834b9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions _printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ int (*printer(char formati))(va_list)
{'S', print_S},
{'p', print_p},
{'u', print_u},
{'x', print_x},
{'X', print_X},
{'\0', NULL}
};
int i = 0;
Expand Down
2 changes: 2 additions & 0 deletions holberton.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ int print_r(va_list arg);
int print_S(va_list arg);
int print_p(va_list arg);
int print_u(va_list arg);
int print_x(va_list arg);
int print_X(va_list arg);
#endif
104 changes: 104 additions & 0 deletions print_hex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "holberton.h"
/**
* print_x - print in hexadecimal function
*@arg: integer to convert
*Return: printed number
*/
int print_x(va_list arg)
{
int n = va_arg(arg, int);
unsigned int nb;
int tab[10];
int i, j, count = 0;
char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
int b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
if (n < 0)
{
_putchar('-');
count++;
nb = -n;
}
else
nb = n;
if (nb == 0)
{
_putchar('0');
count++;
return (count);
}
for (i = 0; nb != 0; i++)
{
tab[i] = nb % 16;
nb = nb / 16;
}
for (; i >= 0; i--)
{
for (j = 0; j < 17; j++)
{
if (tab[i] == b[j])
{
_putchar(a[j]);
count++;
break;
}
}
}
return (count);
}








#include "holberton.h"
/**
* print_X - print in hexadecimal function
*@arg: integer to convert
*Return: printed number
*/
int print_X(va_list arg)
{
int n = va_arg(arg, int);
unsigned int nb;
int tab[10];
int i, j, count = 0;
char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F'};
int b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
if (n < 0)
{
_putchar('-');
count++;
nb = -n;
}
else
nb = n;
if (nb == 0)
{
_putchar('0');
count++;
return (count);
}
for (i = 0; nb != 0; i++)
{
tab[i] = nb % 16;
nb = nb / 16;
}
for (; i >= 0; i--)
{
for (j = 0; j < 17; j++)
{
if (tab[i] == b[j])
{
_putchar(a[j]);
count++;
break;
}
}
}
return (count);
}

0 comments on commit b9834b9

Please sign in to comment.