Re: Decimal to binary error
Since number is defined as an Integer, the maximum value it can store is 32767. If you make it a Long instead, then it can store a much larger number.
Good luck.
Re: Decimal to binary error
That code also has some major "bankers rounding" issues. Since it's a regular division (using /), it's converting to Double, doing the division, and then converting back to Integer (doing bankers rounding).
Just try it with 3, which we should know is 11 in binary. However, it'll report 101 as the answer.
Change it to integer division (using the \) and it'll correct that issue.
Also, just as an FYI, integer division will also work with Long types. If you were to need even more digits than provided by a Long, be careful as integer division won't work with either Currency type nor Decimal type. Either of those would provide quite a few more integer digits than even the Long type.
Re: Decimal to binary error
How's that? Never used that method
Re: Decimal to binary error
I tried what you mentioned above but none worked. I already did the Dec to hex conversation large digits it works only problem is converting to binary 10 decimal digits but can't make it work.
Large integer to binary conversation vb6
Hi everyone!
I'm trying to make this code work for a project calculator I'm making using vb6. The problem is can't convert more than five decimal digits to binary. Ive been trying lot of methods but something tells me I'm missing something. Any help i would appreciate, thanks in advanced.
Code:
Dim number as integer
Dim binary_string as string
Number = val (text1.text)
Binary_string = " "
Do while number > 0
Binary_string = number mod 2 & binary_string
Number = number / 2
Loop
Text2.text = binary_ string
Re: Large integer to binary conversation vb6
You've already asked the exact same question in this thread:
https://www.vbforums.com/showthread....o-binary-error
You were given suggestions, none of which you seem to have implemented since your code above is identical to the code you posted in the other thread. If you had issues with the suggestions you were given, then you should post the code you tried to use that implemented said suggestions and give a detailed description of what the problem was with the updated code.
Re: Decimal to binary error
Merged threads. Please don’t post duplicate threads.
Re: Decimal to binary error
Code:
Function DecToBin$(Value&)
While Value > 0
DecToBin = Value Mod 2 & DecToBin
Value = Value \ 2
Wend
End Function
Re: Decimal to binary error
Hmmm....I wonder what the textbook you are using says. I also wonder if you have asked your professor/teacher/instructor to explain it a bit better, or to provide his/her suggestions.
I am not in favor of writing code for homework assignments. In this case, the suggestions UP TO post #9 were good ones (sorry baka...but I just wouldn't simply give a student his solution...I pity the company he/she works for after 'graduating.'.
Re: Decimal to binary error
its just a \ if u compare, and members already told the OP about it. so nothing new, just show how u can make it into a function.
Re: Decimal to binary error
As a class assignment I wouldn't expect using Val() to be acceptable.
More likely the String of decimal digits is supposed to be converted manually to a numeric type like Long before being converted back to a String of binary digits.
Anything less is just sad.
Re: Decimal to binary error
Code:
I tested the solution you have been offered and it seems to work fine.
Private Function binaryString(number As Long) As String
Dim binary_string As String
10 Do While number > 0
20 binary_string = number Mod 2 & binary_string
30 number = number \ 2
40 Loop
50 binaryString = binary_string
End Function