|
-
Aug 13th, 2022, 05:45 PM
#1
Thread Starter
Member
Decimal to binary error
I've been trying to fix this code for a school project but still can't. Please help I'm newbie. Need to convert max of 10 decimal numbers to binary but this code only limited to 4 dec digits.
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
End sub
Last edited by dday9; Aug 16th, 2022 at 07:29 AM.
-
Aug 13th, 2022, 05:50 PM
#2
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.
-
Aug 13th, 2022, 08:50 PM
#3
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.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Aug 15th, 2022, 05:09 PM
#4
Thread Starter
Member
Re: Decimal to binary error
How's that? Never used that method
-
Aug 15th, 2022, 05:13 PM
#5
Thread Starter
Member
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.
-
Aug 15th, 2022, 06:07 PM
#6
Thread Starter
Member
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
Last edited by dday9; Aug 16th, 2022 at 07:29 AM.
-
Aug 15th, 2022, 06:12 PM
#7
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.
-
Aug 15th, 2022, 08:50 PM
#8
Re: Decimal to binary error
Merged threads. Please don’t post duplicate threads.
-
Aug 16th, 2022, 05:45 AM
#9
Re: Decimal to binary error
Code:
Function DecToBin$(Value&)
While Value > 0
DecToBin = Value Mod 2 & DecToBin
Value = Value \ 2
Wend
End Function
-
Aug 16th, 2022, 02:17 PM
#10
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.'.
Sam I am (as well as Confused at times).
-
Aug 16th, 2022, 03:26 PM
#11
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.
-
Aug 16th, 2022, 08:00 PM
#12
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.
-
Aug 17th, 2022, 01:07 AM
#13
Fanatic Member
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|