Hi I have an binary of 10 bits . How do i change it to decimal
suppose binary is 1000000001 . I need to get the decimal of it . Can some body gimme the psedocode??
Hi I have an binary of 10 bits . How do i change it to decimal
suppose binary is 1000000001 . I need to get the decimal of it . Can some body gimme the psedocode??
Binary 1000000001 = 513 (12^9 + 12^0 = 512 + 1). The simple, reliable method is what described in words: start at 0 and for each bit do value = value * 2 + bit. That avoids pow() and index/off-by-one confusion.
Common mistakes seen above (for example in posts by and ): using decimal += 2 * i treats i as a multiplier instead of an exponent; using pow(2,k) from math.h produces doubles and possible rounding; and reading bits with repeated scanf("%d") often reverses order or requires separators. Treat the input as a string and scan left-to-right, validating each character.
A concise, robust C example (reads one binary string, validates, converts using bit shifts, reports invalid/overflow):
#include <stdio.h>
#include <string.h>
#include <limits.h>
int bin_to_ull(const char *s, unsigned long long *out) {
unsigned long long v = 0;
for (; *s; ++s) {
if (*s == '0') {
if (v > (ULLONG_MAX >> 1)) return 0; // overflow
v <<= 1;
} else if (*s == '1') {
if (v > (ULLONG_MAX >> 1)) return 0; // overflow
v = (v << 1) | 1ULL;
} else {
return 0; // invalid char
}
}
*out = v;
return 1;
}
int main(void) {
char buf[128];
if (!fgets(buf, sizeof buf, stdin)) return 1;
buf[strcspn(buf, "\n")] = '\0';
unsigned long long val;
if (!bin_to_ull(buf, &val)) { printf("invalid or overflow\n"); return 1; }
printf("%llu\n", val);
return 0;
} Notes: for a fixed 10-bit input there is no overflow risk; for longer binaries use the overflow check above or strtoull(buf, &end, 2) if available. Refer to and for other snippet links, but the string+shift approach shown here is the safest and easiest to reason about.
Jump to Post— Salem 6,009So figure out how you turn "1234" into an integer on paper.
"1234"(decimal) is 1234
"1234"(octal) is 668It's just a loop of extracting the numeric value of each digit (say '3' into 3), and multiplication by the base (10, 8, 2 or whatever).
Jump to Post— Shaffer 0Here, Google's wonders:
http://mistupid.com/computers/binaryconv.htmOr do you want the source code to that?
Jump to Post— VatooVatoo 21read this: http://www.daniweb.com/code/snippet109.html
So figure out how you turn "1234" into an integer on paper.
"1234"(decimal) is 1234
"1234"(octal) is 668
It's just a loop of extracting the numeric value of each digit (say '3' into 3), and multiplication by the base (10, 8, 2 or whatever).
Here, Google's wonders:
http://mistupid.com/computers/binaryconv.htm
Or do you want the source code to that?
Ok try this,
int x[11];
int i=11,decimal=0;
for(i=0;i<11;i++)
/*here to allow the user to enter 11 binary numbers*/
scanf("%d",&x);
for(i=10;i>=0;i--)
{
if(a==1)
decimal= decimal + 2 * i ;
}
printf("your decimal number is %d",decimal);
please reply to me if it is correct thanks
int x[11];
int i=11,decimal=0;
for(i=0;i<11;i++)
/*here to allow the user to enter 11 binary numbers*/
scanf("%d",&x[i]);
for(i=10;i>=0;i--)
{
if(a[i]==1)
decimal+=2 * i ;
}
printf("your decimal number is %d",decimal); It is working....:cool:
sorry jokerjokerer what I have done is not correct How!!!!.
let's say we have this binary number00000000001 by the privous programming gives the result equal to 2048 and that is wrong the correct decimal is 1 not 2048 . therefore I would correct my answer by the following programe
and please if you get it reply to me thanks
#include<stdio.h>
#include<math.h>
int main()
{
int k=-1,i=0,decimal=0;
int a[11];
printf("please enter 11 binary numbers");
for(i=0;i<11;i++)
//to allow user enter 11 binary numbers
scanf("%d",&a);
if(a==1)
{
k++;
decimal+=power(2,k);
}
else
k++;
return 0;
}
//please as soon as you get it tell me that.
// sorry for my mistak
sorry I missed the for looping after the scanf
for(i=10;i>=0;i--){
// put here the program as mentioned above
}
return 0;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.