Created
March 28, 2022 13:29
-
-
Save ntfargo/e6bb814911aa9b56196717ea6b285009 to your computer and use it in GitHub Desktop.
Binary to decimal converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
// binary to decimal | |
int bin_to_dec(char *bin) { | |
int i, dec = 0, len = strlen(bin); | |
for (i = 0; i < len; i++) { | |
dec += (bin[i] - '0') * pow(2, len - i - 1); | |
} | |
return dec; | |
} | |
int main() { | |
char binarynum[] = "1000101"; | |
printf("%d\n", bin_to_dec(&binarynum)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment