# Thread: [RESOLVED] decimal to binary

1. ## [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:
```Dim Number As Integer
Dim Binary_String As String
Number = Val(Text1.Text)
Binary_String = ""
While Number > 0
Binary_String = Str(Number Mod 2) & Binary_String
Number = Number \ 2
Wend
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. ## Re: decimal to binary

Try using Trim() in place of Str().

3. ## Re: decimal to binary

Also, ALL spaces can be eliminated with Replace() function.
VB Code:
`Text2.Text = Replace(Binary_String, Space(1), "")`

4. ## Re: decimal to binary

yes trim() works thanks

5. ## Re: [RESOLVED] decimal to binary

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

6. ## Re: [RESOLVED] decimal to binary

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

7. ## Re: [RESOLVED] decimal to binary

VB 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```
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. ## Re: [RESOLVED] decimal to binary

yeah now I see it, tnx man!

9. ## 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. ## 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.

11. ## 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. ## 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.

13. ## Re: [RESOLVED] decimal to binary

Originally Posted by SmUX2k
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?
I see no logical reason why stuff like that should be discouraged.

Olaf

14. ## 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?

15. ## Re: [RESOLVED] decimal to binary

Originally Posted by Tabi
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. ## Re: [RESOLVED] decimal to binary

Understood, that's what I'll do

17. ## 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.

18. ## 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...

19. ## 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. ## Re: [RESOLVED] decimal to binary

Originally Posted by Eduardo-
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. ## 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. ## Re: [RESOLVED] decimal to binary

Originally Posted by Eduardo-
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.

23. ## Re: [RESOLVED] decimal to binary

Originally Posted by Tabi
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. ## Re: [RESOLVED] decimal to binary

Originally Posted by Eduardo-
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•