Skip to content

Instantly share code, notes, and snippets.

@ntfargo
Created March 28, 2022 13:29
Show Gist options
  • Save ntfargo/e6bb814911aa9b56196717ea6b285009 to your computer and use it in GitHub Desktop.
Save ntfargo/e6bb814911aa9b56196717ea6b285009 to your computer and use it in GitHub Desktop.
Binary to decimal converter
#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