Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Programming (https://www.askmehelpdesk.com/forumdisplay.php?f=437)
-   -   Visual basic binary converter (https://www.askmehelpdesk.com/showthread.php?t=132624)

  • Sep 22, 2007, 07:36 PM
    Manu68
    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
  • Oct 11, 2007, 07:13 AM
    jstrike
    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.
  • Oct 23, 2007, 02:16 PM
    Manu68
    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

  • All times are GMT -7. The time now is 07:49 PM.