Question

In: Mechanical Engineering

write a VBA code to convert an arbitrary positive binary fractional number (0< number<1) to equivalent...

write a VBA code to convert an arbitrary positive binary fractional number (0< number<1) to equivalent decimal number. the code should acquire the binary fraction number in the format"0.xxxxxx"from input box, then return the equivalent decimal number in a message box. in the code, you may need to use VBA function "mid(_,_,_)" to pick up a specific symbols or characters from a string. you can use below conversion as benchmark to verify and debug your code:

(0.1011)2 = (0.6875)10

  

Solutions

Expert Solution

Sub Binary_to_Decimal()
Dim B, B1, B2 As String
Dim i, A As Integer
Dim A1, D As Double

B = InputBox("please enter the Fractional binary number(0.xxxxxx)")
'B = 0.1011
B1 = Mid(B, 1, 1)
B2 = Mid(B, 2, 1)
If B1 = 0 And B2 = "." Then
D = 0
    For i = 1 To Len(B) - 2
        A = Mid(B, i + 2, 1)
        A = CInt(A)
        If A = 0 Or A = 1 Then
        A1 = Application.WorksheetFunction.Power(2, -i)
        D = D + A * A1
        Else: GoTo 1
        End If
    Next i
    MsgBox ("The equivalent Decimal number for given Fraction Binary number is equal to " & D)
Else
1       MsgBox ("Input is not in correct format")
End If
End Sub


Related Solutions

Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer...
6.28 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
1) Write a method that takes a string representing a positive binary number, and writes out...
1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.) 2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters. 3) Write an encodeString method that encodes a string of ASCII Characters. Input: It takes two parameters: a) inputString:...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
In VBA, write a code that does as follows: The first worksheet ("Ex. 1") has a...
In VBA, write a code that does as follows: The first worksheet ("Ex. 1") has a list of 50 numbers. Write a program that will read them into an array, then will calculate and output the following: - How many of the numbers are even (output in E2) - How many of the numbers are greater than 300 (output in E3) - The average of the numbers (output in E4) - In column B, output next to each number, the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Construct NFA of following languages and convert it to equivalent DFA. The set of all binary...
Construct NFA of following languages and convert it to equivalent DFA. The set of all binary strings such that 3th symbol from right end is 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT