-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv_utils2.c
76 lines (64 loc) · 1.04 KB
/
conv_utils2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "holberton.h"
/**
* _uitoa - converts an unsigned int to an ascii string
*
* @i: int to convert
*
* Return: a string containing the number in ascii chars
*/
char *_uitoa(unsigned int i)
{
char *str = NULL;
unsigned int n = i;
int j = 0;
str = malloc(sizeof(char) * 11);
if (!str)
{
free(str);
return (NULL);
}
while (n / 10)
{
str[j++] = (n % 10) + '0';
n /= 10;
}
str[j++] = (n % 10) + '0';
str[j] = '\0';
rev_string(str);
return (str);
}
/**
* _ptrtohex - converts pointer address to hex and stores in string
*
* @ptr: the pointer to convert
*
* Return: a string containing the number in hexadecimal
*/
char *_ptrtohex(void *ptr)
{
unsigned long quo, rem;
int j = 0;
char res[10] = "(nil)";
char *ret;
quo = (unsigned long)ptr;
while (quo != 0)
{
rem = quo % 16;
if (rem < 10)
res[j++] = 48 + rem;
else
{
res[j++] = 87 + rem;
}
quo /= 16;
}
if (ptr != NULL)
{
res[j++] = 'x';
res[j++] = '0';
res[j++] = '\0';
rev_string(res);
}
ret = _strdup(res);
return (ret);
}