Ask Experts Questions for FREE Help !
Ask
    Manu68's Avatar
    Manu68 Posts: 2, Reputation: 1
    New Member
     
    #1

    Sep 22, 2007, 07:36 PM
    Visual basic binary converter
    Hello
    I need to create a converter from decimal to binary but the way it works it is not the normal way, if for example I enter the number 80, I do not want to have this number in binary (1010000) but I want to have 8 in binary and then add the 0 in binary to it, which will be 100000000. Then I will need to separate this number in groups of 4 (10000 0000) and compare the first 3 numbers of each generated number to a certain pattern to see if it is correct or not. The number entered can be any size and any decimal number. For the first part I wrote this code but it does not work:
    Private Sub Command1_Click()
    Dim num As String
    Dim I As Integer
    Dim numero

    num = Text1.Text
    I = 1
    Do While I <= Len(num)
    numero = Mid(num, I, 1)
    Label1.Caption = LongToBin(numero)
    I = I + 1
    Loop
    End Sub

    Function LongToBin(ByVal bin_num As Long) As String
    Dim text As String
    Dim I As Integer

    Do While bin_num >= 1
    If bin_num Mod 2 = 1 Then
    text = "1" & text
    Else
    text = "0" & text
    End If
    bin_num = bin_num \ 2
    Loop
    LongToBin = text
    If bin_num = 0 Then
    text = text + "0000"
    End If
    End Function
    Can someone help me with this please?
    Thanks a lot
    jstrike's Avatar
    jstrike Posts: 418, Reputation: 44
    Full Member
     
    #2

    Oct 11, 2007, 07:13 AM
    Just to make sure I have this right, you want to take a number, 623 for example, and convert each digit to binary rather than the number as a whole. So for our example 623 you would have 0110 0010 0011.

    You'll have to forgive me because my VB is very rusty, I just hacked this out in Java.
    Code:
    //Declare an array of integers 8,4,2,1 as a constant
    //In VB it's something like: Dim pows as Integer() = new Integer() {8,4,2,1}
    private static final int pows[] = {8,4,2,1};
    
    //function to convert the string to binary.
    //The VB code for this would be: Function intToBin(ByVal strNum As String) As String
    private static final String intToBin(String strNum) {
      String retVal = "";
      int binNum=0;
    	
      //Convert the string to an integer, if an error occurs return binary 0's 
      //You would have to use an onError to accomplish this functionality in VB.
      try {
        binNum=Integer.parseInt(strNum);
      }catch(Exception e) {
        return "0000";
      }
    
      //convert the number to binary
      //loop over the array we created...
      //In vb this is just a for/next loop
      //for r=0 to uBound(pows)-1
      for(int r=0;r=pows[r]) {
          retVal += "1";
          binNum=binNum-pows[r];
        } else {
          retVal+="0";
        }
      }
    
      //return the value to the calling routine.
      //for VB: intToBin = retVal
      return retVal;
    }
    Hope this helps, let me know if you have any questions.
    Manu68's Avatar
    Manu68 Posts: 2, Reputation: 1
    New Member
     
    #3

    Oct 23, 2007, 02:16 PM
    Quote Originally Posted by Manu68
    Hello
    I need to create a converter from decimal to binary but the way it works it is not the normal way, if for example I enter the number 80, i do not want to have this number in binary (1010000) but I want to have 8 in binary and then add the 0 in binary to it, which will be 100000000. Then I will need to separate this number in groups of 4 (10000 0000) and compare the first 3 numbers of each generated number to a certain pattern to see if it is correct or not. The number entered can be any size and any decimal number. For the first part I wrote this code but it does not work:
    Private Sub Command1_Click()
    Dim num As String
    Dim i As Integer
    Dim numero

    num = Text1.Text
    i = 1
    Do While i <= Len(num)
    numero = Mid(num, i, 1)
    Label1.Caption = LongToBin(numero)
    i = i + 1
    Loop
    End Sub

    Function LongToBin(ByVal bin_num As Long) As String
    Dim txt As String
    Dim i As Integer

    Do While bin_num >= 1
    If bin_num Mod 2 = 1 Then
    txt = "1" & txt
    Else
    txt = "0" & txt
    End If
    bin_num = bin_num \ 2
    Loop
    LongToBin = txt
    If bin_num = 0 Then
    txt = txt + "0000"
    End If
    End Function
    Can someone help me with this please?
    Thanks a lot
    Thank you very much for the answer
    Regards

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Visual Basic [ 3 Answers ]

hi, I am using Access as my DB. I want to save a value in one table in DB. But the value am saving shd exists in database in another table. i.e. that value hs to be compared with the other table .Only if it exists then it has to be saved. How to do it.

Visual basic [ 1 Answers ]

Where do I go on my computer to find out what version of visual basic I have? Thanks

Visual basic [ 1 Answers ]

I am doing visual basic program. Could you help with the code how to login.

Visual basic [ 1 Answers ]

How can I disable maximise button of a form but minimise and close button should be enable?


View more questions Search