Convert binary to decimal | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

int binary_to_decimial(char *str) {

int length = strlen(str);
int sum = 0;

for (int i = 0; i < length; i++) {
int asciival = (int)str[i];
int result;

if (asciival == 49) asciival = 1;
else if (asciival == 48) asciival = 0;

result = asciival * pow(2, length - i - 1);
sum += result;
}
return sum;
}
// I wrote this function instead that I want to share with you to see what you think about it. Also thank you a lot for this series. I am watching every single one of these videos. It is teaching me how to think like a programmer.

nabihalkhateeb
Автор

this helped big time, also since i was working with a 32 bit binary number I could pass additional variables into the function to tell it where to start and stop reading the binary letting me convert certain parts of it saving me having to mess around with masks

mainchannel
Автор

The best and most efficient way to get it done, Thanks :
// An alternative method
int binary_to_decimal(const char *binary){
int length = strlen(binary);
if(binary == NULL || length > 32) return 0;
int decimal = 0;
while(*binary != '\0'){
int place_value = 1;
for(int i = 0; i < length-1; i++){
place_value*=2;
}
decimal += place_value*(*binary-'0');
--length;
++binary;
}
return decimal;
}

justcurious
Автор

why did you chose a pointer char to the string instead of using char array (the string itself) as a parameter?

bushraw
Автор

@Portfolio

I tried this on a a Windows box and for some reason instead of getting the correct answer back it is giving me the full number of the bits as if they are all '1'. I tried it with '10101' and got back 32. and I used a second with '8 bits' which was like '00110100' but I got 256 back as the answer.

You have any thought on this??
I am truly new to actually trying C programming. I am doing what you do in your vids just to get familiar with it. And I have had those errors and warnings come up. So I'm definitely having to deal with that stuff.

OH! and what's interesting is that math work inside the array braces [ ] and how it works on the left side where the rariable is.

The thing that gets my curiosity is how does 1 variable do the work on both sides? ...[i] = ....[i] Especially with the +/- involved?

cleightthejw
Автор

If I wanted to get the signed decimal value how would I go about doing that?

sanighiabalantine
Автор

Really helpful video!!! can you make a video on how to add leading zeros when comparing too binary numbers from user input?

EvanWelborn
Автор

I have a question
How can we ask user the binary number and print the output?
I am new in C. Went through the playlist and got everything related to c Thanks.

PhamousBoi
Автор

int BiToDec(char *string){

// declaring the neeeded variables

int slen = strlen(string);
int total = 0;
int decval = 1;

// a reverse for loop to calculate the decimal number

for(int i =(slen -1); i >= 0; i--)
{
if (string[i] == '1')
{
total += decval;
}
decval *=2;

}
total /=2;


return total;

}


int main(){

//declaring the input variable and its size

char input[25];

//getting the input from the user

printf("type in your Binary number: ");
fgets(input, 25, stdin);

//assagin the function to a variable and than printing it

int value = BiToDec(input);
printf("%s, In decimal is %d", input, value);





return 0;
}


So this is my code, for this and i don't know why but when trying to make it so that the input is from the user, it doubles the total of the number, now i fixed it dividing by two, but i still wonder why does this problem come.

WHITEWOLF-opoo
Автор

int bin_to_dec(const char* bin) {
size_t bin_length = strlen(bin);
int i = 0;
int dec = 0;

while (bin[i] != '\n') {
if (bin[i] == '1') {
dec += (int)pow(2, bin_length - (i + 1));
}
i++;
}

return dec;
}

deepblackoutlaw