answersLogoWhite

0


Best Answer

First of all we will talk about how binary number are converted back into decimal representation and later we will have program.

Here is the formula of this transformation:

Binary number: a3a2a1a0

Decimal number a x 23 + a x 22 + a x 21 + a x 20

Example:

Binary: 1101

Decimal: 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 8 + 4 + 0 + 1 = 13

And here we have our program:

#include

#include

#include

int main() {

char str[100];

int ind;

int sum = 0;

printf("Please enter binary number: ");

scanf("%s", str);

for(ind = 0; ind < strlen(str); ind++) {

sum += (str[ind] - 0x30) * pow(2, strlen(str) - ind - 1);

}

printf("Number in decimal would be %d\n", sum);

return 0;

}

Testing:

Please enter binary number: 1101

Number in decimal would be 13

Please enter binary number: 10000001

Number in decimal would be 129

Please enter binary number: 11111111

Number in decimal would be 255

Please enter binary number: 0

Number in decimal would be 0

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

On a binary computer, all numbers (in fact all data) are binary to begin with so there is no conversion required. Even when you input decimal values, those values must be converted to binary. When you print those same values, they are converted back to decimal. So what you really mean to ask is how we display those binary values using binary notation rather than decimal.

For signed integer values, a one-to-one conversion is the easiest to implement. The following shows a typical implementation in C:

void PrintBinary(int decimal)

{

std::cout << "Decimal " << decimal << " = binary ";

if( decimal < 0 )

{

// Print the sign and convert to positive.

std::cout << '-';

decimal *= -1;

}

// Initialise variables.

int bit = decimal;

int bits = 1;

// Count the binary digits.

while( bit >>= 1 )

++bits;

// Start with most-significant binary digit and work right.

bit = 1 << bits;

while( bit >>= 1 )

std::cout << ( bit & decimal ? '1' : '0' );

std::cout << std::endl;

}

In assembler, the above equates to to the following:

void PrintBinary(int decimal)

{

00F62380 push ebp

00F62381 mov ebp,esp

00F62383 sub esp,0D8h

00F62389 push ebx

00F6238A push esi

00F6238B push edi

00F6238C lea edi,[ebp-0D8h]

00F62392 mov ecx,36h

00F62397 mov eax,0CCCCCCCCh

00F6239C rep stos dword ptr es:[edi]

std::cout << "Decimal " << decimal << " = binary ";

00F6239E push offset string " = binary " (0F6BBB0h)

00F623A3 mov esi,esp

00F623A5 mov eax,dword ptr [decimal]

00F623A8 push eax

00F623A9 push offset string "Decimal " (0F6BBA4h)

00F623AE mov ecx,dword ptr [__imp_std::cout (0F6F3D0h)]

00F623B4 push ecx

00F623B5 call std::operator<<<std::char_traits<char> > (0F6129Eh)

00F623BA add esp,8

00F623BD mov ecx,eax

00F623BF call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0F6F37Ch)]

00F623C5 cmp esi,esp

00F623C7 call @ILT+800(__RTC_CheckEsp) (0F61325h)

00F623CC push eax

00F623CD call std::operator<<<std::char_traits<char> > (0F6129Eh)

00F623D2 add esp,8

if( decimal < 0 )

00F623D5 cmp dword ptr [decimal],0

00F623D9 jge PrintBinary+74h (0F623F4h)

{

// Print the sign and convert to positive.

std::cout << '-';

00F623DB push 2Dh

00F623DD mov eax,dword ptr [__imp_std::cout (0F6F3D0h)]

00F623E2 push eax

00F623E3 call std::operator<<<std::char_traits<char> > (0F61474h)

00F623E8 add esp,8

decimal *= -1;

00F623EB mov eax,dword ptr [decimal]

00F623EE imul eax,eax,0FFFFFFFFh

00F623F1 mov dword ptr [decimal],eax

}

// Initialise variables.

int bit = decimal;

00F623F4 mov eax,dword ptr [decimal]

00F623F7 mov dword ptr [bit],eax

int bits = 1;

00F623FA mov dword ptr [bits],1

// Count the binary digits.

while( bit >>= 1 )

00F62401 mov eax,dword ptr [bit]

00F62404 sar eax,1

00F62406 mov dword ptr [bit],eax

00F62409 je PrintBinary+96h (0F62416h)

++bits;

00F6240B mov eax,dword ptr [bits]

00F6240E add eax,1

00F62411 mov dword ptr [bits],eax

00F62414 jmp PrintBinary+81h (0F62401h)

// Start with most-significant binary digit and work right.

bit = 1 << bits;

00F62416 mov eax,1

00F6241B mov ecx,dword ptr [bits]

00F6241E shl eax,cl

00F62420 mov dword ptr [bit],eax

while( bit >>= 1 )

00F62423 mov eax,dword ptr [bit]

00F62426 sar eax,1

00F62428 mov dword ptr [bit],eax

00F6242B je PrintBinary+0CDh (0F6244Dh)

std::cout << ( bit & decimal ? '1' : '0' );

00F6242D mov eax,dword ptr [bit]

00F62430 and eax,dword ptr [decimal]

00F62433 setne cl

00F62436 add cl,30h

00F62439 movzx edx,cl

00F6243C push edx

00F6243D mov eax,dword ptr [__imp_std::cout (0F6F3D0h)]

00F62442 push eax

00F62443 call std::operator<<<std::char_traits<char> > (0F61474h)

00F62448 add esp,8

00F6244B jmp PrintBinary+0A3h (0F62423h)

std::cout << std::endl;

00F6244D mov esi,esp

00F6244F mov eax,dword ptr [__imp_std::endl (0F6F3CCh)]

00F62454 push eax

00F62455 mov ecx,dword ptr [__imp_std::cout (0F6F3D0h)]

00F6245B call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0F6F3D4h)]

00F62461 cmp esi,esp

00F62463 call @ILT+800(__RTC_CheckEsp) (0F61325h)

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Assuming the binary number is given as a string and the octal number must be, too:

Initalise a result value to zero.

For each digit in the binary number, from right to left:

If the digit is a '1':

Add (1<


While the result value is nonzero:

Compute the result value modulo 8.

This modulus is the current octal digit, from right to left. Prepend the number to a result string.

Subtract the modulus from the result value.

Divide the result value by 8.


The result string now holds the octal representation of the number. Since one octal digit corresponds to three binary digits, the width of the octal number will be a third of the width of the binary number.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Use the following algorithm (written in pseudo-code):

let string be a binary string (e.g., "10110100")

let value be 1

let accumulator be 0

for all positions string.length()-1 to 0 inclusive (e.g., work backwards through string)

{

if string[position] == '1' then add value to accumulator

double the value (e.g., left-shift value by 1 bit, or multiply by 2)

}

end for

print accumulator (e.g., prints 180)

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A binary computer cannot represent decimal values using decimal notation, everything has to be converted to binary. Conversion between decimal and binary is completely automatic.

When we enter a decimal value, such as 42, we are actually entering the individual characters ('4' and '2'). That is, the value 42 is represented by the character sequence "42". This character sequence is not a number, but it does have a binary representation, 0x3432. This value is composed from the equivalent ASCII character codes 0x34 ('4') and 0x32 ('2').

Note that we (humans) use hexadecimal notation rather than binary notation as it is more convenient. 0x3432 in binary is 0011010000110010, however 0011010000110010 is not 42 decimal, it is in fact 13,362 decimal.

To convert the character sequence 0x3432 to an actual decimal value (rather than a character sequence), the computer first converts each character from its ASCII character representation to its numeric representation. This is achieved by subtracting 0x30 from each character:

0x34 - 0x30 = 0x04

0x32 - 0x30 = 0x02

0x02 represents two units, so we multiply this by 10^0 which is 0x01 in binary.

0x02 * 0x01 = 0x02

0x04 represents four tens, so we multiply by 10^1 which is 0x0A in binary.

0x04 * 0x0A = 0x28

These two values are then summed:

0x02 + 0x28 = 0x2A

Thus the character sequence "42" becomes 0x2A, which is 00101010 in binary, which is 42 decimal.

In order for the computer to present the binary value 00101010 in decimal (to the user), it must convert the binary value 0x2A back to its character representation 0x3432. This is achieved as follows:

0x2A % 0x0A = 0x02

In decimal, this is the same as saying 42 % 10 = 2. That is, we divide by 10 and take the remainder.

0x02 is the low-order digit. To get the next lowest-order digit, we subtract 0x02 from 0x2A:

0x2A - 0x02 = 0x28

This is the same as 42 - 2 = 40 in decimal.

We then divide 40 by 10, which is 4:

0x28 / 0x0A = 0x04

And finally take the modulo of 10:

0x04 % 0x0A = 0x04

There are no more digits, so we can begin composing the character sequence by adding the value 0x30 to each digit:

0x04 + 0x30 = 0x34

0x02 + 0x30 = 0x32

Thus 0x2A (42 decimal) becomes 0x3432 (the character sequence "42").

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

' Declare the functions

DECLARE FUNCTION Bin$ (decimal&)

DECLARE FUNCTION Dec& (Binar$)

CLS

' Convert 255 into "11111111"

alt$ = Bin$(255)

PRINT alt$

' Convert "11111111" back to 255

PRINT Dec&(alt$)

FUNCTION Dec& (Binary$)

' Convert a binary string to a decimal number...

decimal& = 0: power = 0

Binary$ = UCASE$(Binary$)

FOR re = LEN(Binary$) TO 1 STEP -1

dig = ASC(MID$(Binary$, re, 1)) - 48

IF dig < 0 OR dig > 1 THEN decimal& = 0: EXIT FOR

decimal& = decimal& + dig * 2 ^ (power)

power = power + 1

NEXT

Dec& = decimal&

END FUNCTION

FUNCTION Bin$ (decimal&)

' Convert a long integer to a binary string...

Bn$ = ""

h$ = HEX$(decimal&)

FOR re = 1 TO LEN(h$)

dig = INSTR("0123456789ABCDEF", MID$(h$, re, 1)) - 1

IF dig < 0 THEN Bn$ = "": EXIT FOR

j = 8

k = 4

DO

Bn$ = Bn$ + RIGHT$(STR$((dig \ j) MOD 2), 1)

j = j - (j \ 2)

k = k - 1

IF k = 0 THEN EXIT DO

LOOP WHILE j

NEXT

Bin$ = Bn$

END FUNCTION

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The main problem with binary to decimal conversion is that whenever we input a number, that number is assumed to be a decimal value, not a binary value. This is simply because users find decimal much easier to work with. To input a binary value we have to input a string, thus the question becomes "How do we convert the string representation into a binary value?" Once we have stored the binary value, we can convert to decimal simply by printing the stored value.

To convert a binary value stored as a string into a numeric binary value, we begin by zero-initialising an integer variable. We then loop through the input string, inspecting each character in turn. On each iteration of the loop we multiply the integer variable by 2. We then inspect the current character and if it is a '1' we add 1 to the integer variable. If it is a '0' we do nothing. We then print the variable which gives us the decimal value.

The hardest part is simply taking input from the user because we have to check for any and all input errors. Fortunately, binary values only have two digits (0 and 1), so if the input string contains anything other than a '0' or a '1' character then the input must be invalid.

The following GW BASIC example demonstrates how we can take input from the user and test it for validity whilst converting it to decimal. If any errors occur within the input, we start over.

10 DEFSTR B$ 20 DEFINT N

30 B$=""

40 N=0

50 INPUT "Enter a binary value: ", B$

60 FOR C=0 TO LEN(B$)-1

70 IF (B$(C)='1') or (B$(C)='0') THEN GOTO 100

80 PRINT "Binary value not recognised. Please try again."

90 GOTO 30

100 N=N*2

110 IF B$(C)='1' THEN N=N+1

120 NEXT C

130 PRINT B$ "binary is " N " decimal"

140 END

RUN

Enter a binary value: 12345678

Binary value not recognised. Please try again.

Enter a binary value: 00101010

00101010 binary is 42 decimal

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

While I don't remember enough GW-BASIC to actually do that, in general you should do the following:* Write a loop.
* Within the loop, extract each of the digits, from right to left.

* Multiply the first (rightmost) digit by 1, the next one by 2, the next one by 4, etc.; the multiplier should be multiplied by 2 in each step. Add this product to the total, which should initially be set to zero.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

write a program that will convert decimal to binary

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make binary to decimal converter in G W BASIC?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

BCD to Gray Code Converter?

BCD refers to Binary Code Decimal there are no diagrams it is just a numbers system GRAY code is a means to make one reliable state to change at a time eliminating false coding because of transitions in counters and such


How do you make a digital object counter?

A: First you need a object detector that give out a pulse per each. That becomes the clock for any the input to a counter that will count up in binary coded decimal and of these there are many to choose from. Eventually it will fill up and starts over unless there is a reset along the time of counting. that is the basic.


Explain binary coded decimal?

Binary Coded Decimal (BCD) is a set of coding systems for storing decimal digits in binary code. There are several such codes, I will give examples of 3: straight BCD, XS3 BCD, and 2 of 5 BCD. Straight BCD uses the actual binary value of the decimal digit value: 0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 6 = 0110 7 = 0111 8 = 1000 9 = 1001 XS3 BCD adds 3 to the binary value of the decimal digit value to make the code: 0 = 0011 1 = 0100 2 = 0101 3 = 0110 4 = 0111 5 = 1000 6 = 1001 7 = 1010 8 = 1011 9 = 1100 2 of 5 BCD uses a 5 bit code where only 2 bits can be on in a decimal digit's code: 0 = 00011 1 = 11000 2 = 10100 3 = 10010 4 = 10001 5 = 01100 6 = 01010 7 = 01001 8 = 00110 9 = 00101


What is the significance of hexadecimal What is the significance of binary Who are they used by?

Binary (base-2) and hexadecimal (base-16) are commonly used by programmers. Binary computers only understand binary encodings. That is, all information (both instructions and data) must be converted into a numeric value; digital information. Humans like to use decimal notation whenever possible, but in order to program a computer in its own native language we must convert all values to binary, the only language the computer actually understands. However, binary is difficult to work with because there are only two symbols: 0 and 1. Decimal, on the other hand, has ten symbols, 0 to 9, so we can easily notate all values from 0 to 9 using just one digit. In binary we would need at least 4 digits to notate the same range of numbers. Thus binary numbers tend to be much longer than their decimal equivalents and are difficult for humans to comprehend; a single digit in the wrong place is much harder to spot. Although we can program the computer to convert decimal notation to native binary, this has a runtime cost because there is no direct conversion between decimal and binary notation. But base-2 is directly related to all bases that are themselves a power of 2. Thus quaternary (base-4), octal (base-8) and hexadecimal (base-16) are all directly related to binary and are therefore more easily converted back and forth than is decimal. We use hexadecimal because it has relatively few symbols (16), and each hex digit maps 1:1 with a group of 4 bits. Since 4 bits is half a byte we call hexadecimal digits nybbles. Since two nybbles make a byte, we can represent any group of 8 bits with just two symbols instead of 8 binary digits. Octal is also used because it allows us to map bits in groups of 3, which can be useful in systems that use a 9-bit byte rather than the more common 8-bit byte, but is also useful when we need to work in base-8 itself.


What are two advantages of using binary numbers rather than decimal numbers in a computer system?

Your question is actually flawed...binary system is not used in digital systems... Rather, systems using binary numbers only are called digital systems... It is common knowledge that, digital electronics employs just 2 states (or rather numbers, as mathematicians put it...) the two numbers being '0' and '1'. Obviously, it is easier to design electronic systems dealing with just 2 states...It's majorly this ease, that led to such exponential development in the field of digital electronics. It ios also cheaper to make or produce such systems...

Related questions

What is the binary number of 209.85.153.94?

Just use the Windows calculator, and set it to scientific mode, or use any scientific calculator that supports binary/decimal. In the Windows calculator, make sure you are in decimal, type in each of the four numbers, then select "Binary" to convert to binary. You will have to fill out some of the binary numbers with zeroes to the left (each one must have 8 binary digits).Just use the Windows calculator, and set it to scientific mode, or use any scientific calculator that supports binary/decimal. In the Windows calculator, make sure you are in decimal, type in each of the four numbers, then select "Binary" to convert to binary. You will have to fill out some of the binary numbers with zeroes to the left (each one must have 8 binary digits).Just use the Windows calculator, and set it to scientific mode, or use any scientific calculator that supports binary/decimal. In the Windows calculator, make sure you are in decimal, type in each of the four numbers, then select "Binary" to convert to binary. You will have to fill out some of the binary numbers with zeroes to the left (each one must have 8 binary digits).Just use the Windows calculator, and set it to scientific mode, or use any scientific calculator that supports binary/decimal. In the Windows calculator, make sure you are in decimal, type in each of the four numbers, then select "Binary" to convert to binary. You will have to fill out some of the binary numbers with zeroes to the left (each one must have 8 binary digits).


What are some basic binary questions for beginners?

How many people does it take to make a binary couple ?... 10


Can you make money in binary options trading?

You can make only if you understan basic forex first. binary options based on mostly currencies, you have to know forex techniques by logic or by nature law. then you can make money with binary options.


What is the computer equivalent 00110111 in decimal?

This is probably a binary number. To convert binary to decimal, assuming you have Windows:Open the Windows Calculator (press Windows-R, then type "calc").Make sure it is in scientific mode (menu command: View - Scientific).Click in "Bin" to change to binary mode.Type in your binary number.Clicking on "Dec" will go back to decimal mode (and convert the number you typed to decimal).Note that the Windows calculator only handles this type of calculations for integers.


What is the main function of binary?

to make it easier for computers to calculate. just as your fingers, you need 10 fingers to count to 10 in decimal, while in binary, you only need 2 basic status, left or right, forward or backward, false or true, which is more efficient. another reason is the diode which composed the computer has only two basic status, on or off, corresponding to 1 and 0.


What does the decimal number 255 mean in binary?

255 as a decimal number (also known as a base 10 number) = 11111111 in binary (also known as a base 2 number). In binary, each digit is known as a bit, and 8 bits are known as 1 byte. 255 is the largest (positive) number you can make in binary using only 8 bits (1 byte).


What are the two numbers the binary system uses?

In theory any two symbols would do, but usually they are 1 and 0.So, for example, the binary for three is 11, and for thirteen the binary is 1101.To make conversions, use the calculator that comes with a Windows operating system. Set the View to Scientific, make sure the Dec (for decimal) button is selected, and key in a number. Then selected the Bin (binary) button, and the binary equivalent displays.


What are the advantages and disadvantages of binary code decimal?

Advantage of binary over decimal: information can be recorded and stored in any dichotomous variable: magnetised or not magnetised (most electronic media), pit or no pit (optoelectronic media CDs/DVDs). For decimal it would be necessary to store as 10 different levels of magnetisation or depths of pits. Not so easy to make such a system error-free. Advantage of decimal over binary: fewer "digits" required. Every ten binary digits (1024 values) can be replaced by just a shade more than three decimal digits (1000 values). So the number of digits to be stored is less than a third.


BCD to Gray Code Converter?

BCD refers to Binary Code Decimal there are no diagrams it is just a numbers system GRAY code is a means to make one reliable state to change at a time eliminating false coding because of transitions in counters and such


How do you make a digital object counter?

A: First you need a object detector that give out a pulse per each. That becomes the clock for any the input to a counter that will count up in binary coded decimal and of these there are many to choose from. Eventually it will fill up and starts over unless there is a reset along the time of counting. that is the basic.


How many black holes make one binary system?

well you wouldn't need any to make a black holes to make binary systems but what do make binary systems are two stars.


What are the steps in converting decimal number with fractional part to binary number?

To convert a decimal number to a binary number, you need to have each number be an integer. So, the first step is to make sure your fractional part really is fractional. Convert each integer to binary separately. 1 and 4/5 to binary would be this: 1 -&gt; 1 4 -&gt; 100 5 -&gt; 101 1 and 100/101 or 1001/101 If it's already in decimal form (instead of 1 and 4/5, you have 1.8), then there's no easy way to do it that I know of. This is partly because the only decimals in binary that don't continue on forever are those which, in fractions form, have denominators made only of 2's. 1/2, 1/4, 3/4, 1/8 and so on.