Results 1 to 24 of 24

Thread: [RESOLVED] decimal to binary

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Resolved [RESOLVED] decimal to binary

    i am trying to find a way to convert decimal to binary in vb, i searched around and found:
    VB Code:
    1. Dim Number As Integer
    2. Dim Binary_String As String
    3.  
    4. Number = Val(Text1.Text)
    5. Binary_String = ""
    6. While Number > 0
    7.     Binary_String = Str(Number Mod 2) & Binary_String
    8.     Number = Number \ 2
    9. Wend
    10. Text2.Text = Binary_String

    this works fine except for it leaves spaces in between each character when i run it, what is wrong with this code?

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: decimal to binary

    Try using Trim() in place of Str().
    This world is not my home. I'm just passing through.

  3. #3

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: decimal to binary

    yes trim() works thanks

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] decimal to binary

    you don't even need trim
    VB Code:
    1. Binary_String = Number Mod 2 & Binary_String
    will suffice

  6. #6
    Lively Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    71

    Re: [RESOLVED] decimal to binary

    Hi, can sb explain the code for decimal -> binary?

  7. #7
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] decimal to binary

    VB Code:
    1. Dim Number As Integer
    2. Dim Binary_String As String
    3.  
    4. Number = Val(Text1.Text)
    5. Binary_String = ""
    6. Do While Number > 0
    7.     Binary_String = Number Mod 2 & Binary_String
    8.     Number = Number \ 2
    9. Loop
    10. Text2.Text = Binary_String
    This is taking an integer and creating a string of 0s and 1s for the binary representation of that number.

    For example, decimal 13 is binary 1101.
    Within the loop, see if the last bit is 1 or 0. Number Mod 2 gives this result directly, as does Number And 1. Add this to the front of the string, then divide the number by 2, ignoring remainder.
    13 Mod 2 = 1; Binary_String = "1"
    13 \ 2 = 6; Loop
    6 Mod 2 = 0; Binary_String = "01"
    6 \ 2 = 3; Loop
    3 Mod 2 = 1; Binary_String = "101"
    3 \ 2 = 1; Loop
    1 Mod 2 = 1; Binary_String = "1101"
    1 \ 2 = 0; Exit Loop

  8. #8
    Lively Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    71

    Re: [RESOLVED] decimal to binary

    yeah now I see it, tnx man!

  9. #9
    New Member webadage's Avatar
    Join Date
    Oct 2017
    Location
    Classified
    Posts
    6

    Re: [RESOLVED] decimal to binary

    FYI: I use Microsoft Visual Basic 6.0 version 8176
    Converting decimal to binary is an easy process using an elementary DO - LOOP
    For example. The following code solves decimal to binary for all numbers 0 to 255.

    This application requires...
    2 forms
    form 1 contains...
    8 image boxes in a control array as 0 - 8
    a few labels
    2 command buttons
    form 2 contains 3 image boxes where i store three digit holders (blank, 0 and 1)

    Here's the code

    Private Sub Command1_Click()
    'NOTE: I DECLARE ALL VARIABLES AS PUBLIC in a separate module for this simple operation

    MyBit = 7 'keeps track of which bit we're working on
    MyValue = Val(Text1.Text) 'entered by user for conversion
    If MyValue < 0 Or MyValue > 255 Then 'check for valid input
    MsgBox "Improper Value. READ THE INSTRUCTIONS!", , "Ooops"
    Exit Sub
    End If
    'How it works: divide starting value by 2 and capture the Integer portion as i and the Remainder portion as r

    'REPEAT THE PROCESS (DO LOOP) UNTIL THE INTEGER PORTION = 0 which means you're done!
    'during the process, IF r ISN'T = 0, SET THE BIT IN PROCESS TO 1. IF r = 0 THEN SET THE BIT IN PROCESS TO 0
    'and IF i = 0 THEN EXIT THE LOOP

    'After entering the number to be converted, the user clicks command1 and the magic happens...
    Do
    i = Int(MyValue / 2)
    r = ((MyValue / 2) - (Int(MyValue / 2)))
    'MsgBox "MyValue= " & MyValue & " i= " & i & " r= " & r
    If r > 0 Then Image1(MyBit).Picture = Form2.Image3.Picture Else Image1(MyBit).Picture = Form2.Image2.Picture
    MyValue = i
    If MyValue = 0 Then Exit Do
    MyBit = MyBit - 1
    Loop
    'MsgBox "DONE!"
    End Sub

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,049

    Re: [RESOLVED] decimal to binary

    In general, it isn't considered polite to resurrect a thread from so long ago. It can cause a few side effects which can be seen as welcome or unwelcome, depending on how one views it. After all, anybody subscribed to the thread will get a notification. In this case, that could cause some interesting results, considering that the thread predates an incident in the intervening years. Not sure how that will play out.

    Having just spent much of the last hour researching something, and stumbling across plenty of threads that had incorrect information due to their age being such that technology has moved on since then, I'm fairly sympathetic to adding something constructive to an old thread, but...as it can step on people's toes.
    My usual boring signature: Nothing

  11. #11
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    521

    Re: [RESOLVED] decimal to binary

    I'd agree with you (no choice, you're moderator, but still I generally accept your decision :-P ) but have you looked into the history of the dead post resurrector in question? They're all posts on threads that are years old before being resurrected.

    Sometimes it is adding something of use (that has probably been done to death in other posts since then, so no need to resurrect the post) but sometimes it is asking a question. As you say, if it is adding something constructive, sure...but it doesn't take long to check https://www.vbforums.com/search.php?searchid=25468968 and find a whole plethora of examples...In their 6 posts, *all* of them are resurrecting old posts from 6+ years ago. Is that a trend you want to allow to continue?

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,049

    Re: [RESOLVED] decimal to binary

    Yeah, but this is VB6. Either everything is out of date, or nothing is. There are threads that have been dormant for years, then come back to life for a time, then go dormant again. For a language that has been out of support for nearly twenty years, by now, nothing is really changing, so everything is potentially current. This forum is likely the best repository for ongoing help for those still using the language.

    It would still be best to start a new thread for a question. That's true in all cases, because one is more likely to get a good answer that way, which is the whole point of asking a question. However, for a reply in this forum, it's much harder to say.
    My usual boring signature: Nothing

  13. #13
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,259

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by SmUX2k View Post
    In their 6 posts, *all* of them are resurrecting old posts from 6+ years ago.
    Is that a trend you want to allow to continue?
    As long as the new question is "On-Topic" and wasn't already answered in the older thread,
    I see no logical reason why stuff like that should be discouraged.

    Olaf

  14. #14
    Member
    Join Date
    Jan 2024
    Location
    Argentina, Santa Fe
    Posts
    48

    Re: [RESOLVED] decimal to binary

    To resurrect an old thread or not?
    It is interesting to analyze the pros and cons

    For example, I am new to the Forum, I see this thread, and I consider that I have a code that could solve the same thing in a better way, and I have no idea if this topic has been discussed before.

    What should I do?
    1) Am I still feeding this old thread?
    2) Should I look for another more recent thread, actually the most recent of all, where the same problem has been raised, although it is also inactive, and post it there?

    Maybe option 2 is correct, but, in the hurry we live in, with little time available, at least that's how my life is, don't do this search, let the opportunity pass by, and don't publish my code

    It would be something like:
    1) Less order and more code
    2) More order and less code

    I don't realize it's the right thing to do

    Sorry, I'm new to the forum, and I don't know if it's correct for me to comment on this.

    At least I have this question. In this case, although the topic is very basic, I have a code that may be useful, I don't know. What should I do?
    Last edited by Tabi; Mar 23rd, 2024 at 03:07 PM.

  15. #15
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    521

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by Tabi View Post
    At least I have this question. In this case, although the topic is very basic, I have a code that may be useful, I don't know. What should I do?
    My opinion is just one of many, but the question here isn't whether your question answers the question in THIS topic or not...it's whether it answers questions that may be asked in future. If the code is potentially useful to people in future, the codebank is the perfect place to post it as a separate thread.

  16. #16
    Member
    Join Date
    Jan 2024
    Location
    Argentina, Santa Fe
    Posts
    48

    Re: [RESOLVED] decimal to binary

    Understood, that's what I'll do

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,946

    Re: [RESOLVED] decimal to binary

    I'm on the side that it doesn't matter. Heck, we may even bring someone back to the forums because they got an email from an old thread.

    Truth be told, I have been snarky once or twice about people resurrecting old threads, but I now think I was wrong.

    And, I don't often resurrect them, but I do fairly often find gems in old threads that help me with stuff.

    So, let the resurrection continue.
    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.

  18. #18
    Addicted Member
    Join Date
    Jul 2021
    Posts
    201

    Re: [RESOLVED] decimal to binary

    In my view, a thread is like a conversation.

    Normally, you do not pick up a conversation years after it ended and continue from there - You start a new conversation.

    Heck, the guys who have started the conversation years ago might not be with us today! Somewhat creepy...
    Last edited by Dry Bone; Apr 2nd, 2024 at 05:32 PM.

  19. #19
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,067

    Re: [RESOLVED] decimal to binary

    One side effect of resurrecting old threads is that when you read them if you are not paying attention to the dates, you think it is a new question and maybe spend some time answering the original post until you realize that the question was asked 20 years ago

  20. #20
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    521

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by Eduardo- View Post
    One side effect of resurrecting old threads is that when you read them if you are not paying attention to the dates, you think it is a new question and maybe spend some time answering the original post until you realize that the question was asked 20 years ago
    And 90% of the time, when you check a resurrected thread, the resurrection is someone saying thanks to the original poster (who probably moved on to .NET before the poster was even born!)

  21. #21
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,408

    Wink Re: [RESOLVED] decimal to binary

    Having resurrected my fair share of dead threads I could say I am pretty well versed in the subtle art of necromancy! Most of the time the resurrection is not in response to the original poster but to add more value to the subject discussed there. If Google deems the thread important enough to show it in the search results it means many others are interested in the subject so opening a new thread would dilute the information.

  22. #22
    Member
    Join Date
    Jan 2024
    Location
    Argentina, Santa Fe
    Posts
    48

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by Eduardo- View Post
    One side effect of resurrecting old threads is that when you read them if you are not paying attention to the dates, you think it is a new question and maybe spend some time answering the original post until you realize that the question was asked 20 years ago
    Perhaps this could be partially solved with a design change of VBForum.

    For example, adding a large horizontal timeline above everything, with marks and their # Posts.

    Or something simpler, such as a gentle change in the background color in the posts, to the extent that a certain amount of time has passed, such as 3 months, without a post having been made.

    Or even simpler, next to # Post, put a Number that indicates the number of days that have passed since the first message. This is instant reading, not like a date, which requires some interpretation.

    Also, since I am new, I don't know if it is implemented, for those who are about to write a new Post, the appearance of a pop-up warning window, that although the thread is not closed, x days passed without new Posts.

    Anyway... I don't know how active the implementation of improvements is in VBForum. We already know, the possible improvements to any Software never end.
    Last edited by Tabi; Apr 3rd, 2024 at 01:52 PM.

  23. #23
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,067

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by Tabi View Post
    I don't know how active the implementation of improvements is in VBForum.
    Unfortunately I guess it is close to zero.
    They don't care about fixing basic things like Unicode characters not showing (they were OK in the past but something screwed them) or problems with attachments.

    It's like we're floating here with parts of a ship that already sank

  24. #24
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,946

    Re: [RESOLVED] decimal to binary

    Quote Originally Posted by Eduardo- View Post
    It's like we're floating here with parts of a ship that already sank
    I think that's true. But I'm always afraid to say too much, for fear they'll just shut it all down. And I'd personally miss this place greatly.
    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.

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