Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Decimal to binary number conversion

Posted on 1998-07-29
3
Medium Priority
?
301 Views
Last Modified: 2008-02-20
Need to convert a decimal to binary.  Tried the following snippet with no luck.  Any body got something more understandable for a VB rookie?

Private Sub Command1_Click()

    DecValue = Val(Text1.Text)  
    BinValue = ""    
    Do
    TempValue = DecValue Mod 2
        BinValue = CStr(TempValue) + BinValue
    DecValue = DecValue \ 2
    Loop Until DecValue = 0     
    'Print
    'Print BinValue     
    Text2.Text = BinValue

End Sub
0
Comment
Question by:regent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 400 total points
ID: 1467185
Here's the same basic code slightly rewritten and made into a function, so that it is easier to understand and debug.  

Public Function ConvertToBinary(DecValue As Long) As String
    Dim BinValue As String
    Do
        BinValue = CStr(DecValue And 1) + BinValue
        DecValue = DecValue \ 2
    Loop Until DecValue = 0
    ConvertToBinary = BinValue
End Function

This function will not work with negative numbers! If you need to print negative numbers, tell me and I'll redo it. If it doesn't work for you otherwise, tell me how and we can debig it.

Hope this helps!
0
 

Author Comment

by:regent
ID: 1467186
I'm confused, tired and in desperation over this stupid conversion.  Can I just e-mail you my VB project and have you take a look at what I'm doing (trying to do)?  I'm at Turbo96TSI@aol.com

Thanks for your help.

tjr
0
 

Expert Comment

by:luvbox
ID: 1467187
VERSION 2.00
Begin Form Form1
   Caption         =   "Form1"
   ClientHeight    =   2070
   ClientLeft      =   1095
   ClientTop       =   1485
   ClientWidth     =   4800
   Height          =   2475
   Left            =   1035
   LinkTopic       =   "Form1"
   ScaleHeight     =   2070
   ScaleWidth      =   4800
   Top             =   1140
   Width           =   4920
   Begin TextBox Text2
      Height          =   285
      Left            =   360
      TabIndex        =   3
      Top             =   1320
      Width           =   4335
   End
   Begin TextBox Text1
      Height          =   285
      Left            =   360
      TabIndex        =   1
      Top             =   480
      Width           =   3015
   End
   Begin Label Label2
      Caption         =   "Display Binary len = 30"
      Height          =   255
      Left            =   360
      TabIndex        =   2
      Top             =   840
      Width           =   2295
   End
   Begin Label Label1
      Caption         =   "Enter Decimal"
      Height          =   255
      Left            =   360
      TabIndex        =   0
      Top             =   120
      Width           =   1935
   End
End
Sub Text1_Change ()
Dim i As Long, x As Long, bin As String
      Const maxpower = 30   ' Maximum number of binary digits supported.

      Text1.MaxLength = 9   ' Maximum number of decimal digits allowed.
      Text2.Enabled = False ' Prevent typing in second text box.
      bin = ""  'Build the desired binary number in this string, bin.
      x = Val(Text1.Text) 'Convert decimal string in text1 to long integer

      If x > 2 ^ maxpower Then
         MsgBox "Number must be no larger than " & Str$(2 ^ maxpower)
         Text2.Text = ""
         Exit Sub
      End If

      ' Here is the heart of the conversion from decimal to binary:

      ' Negative numbers have "1" in the 32nd left-most digit:
      If x < 0 Then bin = bin + "1" Else bin = bin + "0"

      For i = maxpower To 0 Step -1
         If x And (2 ^ i) Then   ' Use the logical "AND" operator.
            bin = bin + "1"
         Else
            bin = bin + "0"
         End If
      Next
      Text2.Text = bin  ' The bin string contains the binary number.

End Sub


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question