-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinary_helper.c
105 lines (84 loc) · 1.72 KB
/
binary_helper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "main.h"
/**
* base_len - Calculates the length for an octal number
* @num: The number for which the length is being calculated
* @base: Base to be calculated by
* Return: An integer representing the length of a number
*/
unsigned int base_len(unsigned int num, int base)
{
unsigned int i;
for (i = 0; num > 0; i++)
{
num = num / base;
}
return (i);
}
/**
* rev_string - reverses a string in place
*
* @s: string to reverse
* Return: A pointer to a character
*/
char *rev_string(char *s)
{
int len;
int head;
char tmp;
char *dest;
for (len = 0; s[len] != '\0'; len++)
{}
dest = malloc(sizeof(char) * len + 1);
if (dest == NULL)
return (NULL);
_memcpy(dest, s, len);
for (head = 0; head < len; head++, len--)
{
tmp = dest[len - 1];
dest[len - 1] = dest[head];
dest[head] = tmp;
}
return (dest);
}
/**
* write_base - sends characters to be written on standard output
* @str: String to parse
*/
void write_base(char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++)
_putchar(str[i]);
}
/**
* _memcpy - copy memory area
* @dest: Destination for copying
* @src: Source to copy from
* @n: The number of bytes to copy
* Return: The _memcpy() function returns a pointer to dest.
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* hex_check - Checks which hex function is calling it
* @num: Number to convert into letter
* @x: Tells which hex function is calling it
* Return: Ascii value for a letter
*/
int hex_check(int num, char x)
{
char *hex = "abcdef";
char *Hex = "ABCDEF";
num = num - 10;
if (x == 'x')
return (hex[num]);
else
return (Hex[num]);
return (0);
}