forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_to_decimal.c
68 lines (58 loc) · 1.75 KB
/
binary_to_decimal.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
/*******************************************************************************
*
* Program: Convert binary to decimal
*
* Description: A function that accepts a string representation of a binary
* number and convers it to decimal (int) in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=jEgiZQLNfiE
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
int convert(char *string);
// Example of binary number conversion logic/math
//
// 10101 binary -> 21 decimal
//
// 1 0 1 0 1
// 2^4 2^3 2^2 2^1 2^0
// 16 + 0 + 4 + 0 + 1
// = 21
//
int main(void)
{
// test the function with some example values
char s1[] = "10101";
int val1 = convert(s1);
printf("s1 in dec: %d\n", val1);
char s2[] = "11111";
int val2 = convert(s2);
printf("s2 in dec: %d\n", val2);
char s3[] = "10000001";
int val3 = convert(s3);
printf("s3 in dec: %d\n", val3);
return 0;
}
// converts the binary number in string to a decimal int value
int convert(char *string)
{
int slen = strlen(string);
// total keeps track of the converted number
int total = 0;
// decval is the current 2^n value of the digit the below loop is converting
int decval = 1;
// We perform the conversion one digit at a time, going from the right side
// of the string to the left. If the current digit is a '1' we add the value
// of that digit to the total. We multiply decval by 2 to keep track of the
// increasing worth of each digit as a power of 2 as we move from right to
// left in the string.
for (int i = (slen - 1); i >= 0; i--)
{
if (string[i] == '1') total += decval;
decval *= 2;
}
return total;
}