Skip to content

Commit f1d72c9

Browse files
committed
A function that converts a binary number to an unsigned int
1 parent 0b2e25b commit f1d72c9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "main.h"
2+
3+
/**
4+
* binary_to_uint - converts a binary number to an unsigned int.
5+
* @b: pointer to a string containing a binary number
6+
*
7+
* Return: unsigned int with decimal value of binsry number, or 0 if error
8+
*/
9+
unsigned int binary_to_uint(const char *b)
10+
{
11+
int i;
12+
unsigned int num;
13+
14+
num = 0;
15+
if (!b)
16+
return (0);
17+
for (i = 0; b[i] != '\0'; i++)
18+
{
19+
if (b[i] != '0' && b[i] != '1')
20+
return (0);
21+
}
22+
for (i = 0; b[i] != '\0'; i++)
23+
{
24+
num <<= 1;
25+
if (b[i] == '1')
26+
num += 1;
27+
}
28+
return (num);
29+
}

0 commit comments

Comments
 (0)