Results 1 to 13 of 13

Thread: Decimal to binary error

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    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.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    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.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Decimal to binary error

    How's that? Never used that method

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    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.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    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.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Decimal to binary error

    Merged threads. Please don’t post duplicate threads.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,001

    Re: Decimal to binary error

    Code:
    Function DecToBin$(Value&)
        While Value > 0
            DecToBin = Value Mod 2 & DecToBin
            Value = Value \ 2
        Wend
    End Function

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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).

  11. #11
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,001

    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.

  12. #12
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  13. #13
    Fanatic Member
    Join Date
    Mar 2019
    Posts
    515

    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
  •  



Click Here to Expand Forum to Full Width