Convert binary to decimal

hi, i was wondering what's the code to convert binary numbers to decimal numbers?

import java.io.*;
class convertBinarytoDecimal{
     public static void main (String args[])
     throws Exception {
          System.out.println("Enter the Binary Number");
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          String str = br.readLine().trim();
          int [] myArray = new int [str.length()];
          for(int i=0;i<myArray.length;i++){
               myArray[i] = Integer.parseInt(str.charAt(i)+"");
          convertBinarytoDecimal(myArray);
     static void convertBinarytoDecimal(int [] myArray){
          int decimal = 0;
          for (int i =0;i<myArray.length;i++){
               decimal += myArray[i] * Math.pow(2,myArray.length-(i+1));
          System.out.println(decimal);
}

Similar Messages

  • Help converting binary to decimal.

    Hi all.
    I'm taking a course in Java programming and so far its been a great experience. I'm currently stumped on one of my assignments and I am kindly requesting some assistance.
    Basically I have to convert a binary entry into its decimal comparison.
    so for instance an entry of 1101 would output 13.
    I have the calculation formula, but my problem is finding out whether the point i'm looking at is either a 1 or a 0. I can't seem to 'strip it down' to the value.
    My current thought is something like this:
    binary = 1101;
    // problem is here
    thou = (binary % 1000) / 1000;
    hund = (thou % 100) / 100;
    tens = (hund % 10) / 10;
    ones = (tens % 1) / 1;
    // then do formular
    decimal = ones * 1 + tens * 2 + hund * 4 + thou * 8;
    I have searched but all the suggestions say to use a custom function or other obscure method. I don't know that stuff real well and we aren't even that far in the book either. All we've done so far is while, if .. else, condition statements.
    I will include the actual text for the question, in case i'm not clear. This text also includes a 'tip' which isn't helping me. (http://www.geocities.com/kaveman2000/q-4-25.pdf)
    Thanks again for any pointers.

    angeles1016 (and all),
    thanks for your great help and code snippets.
    angeles1016,
    using the code you provided i noticed that the last (single digit) was giving the wrong response in certain cases. (1000, 0111, 0001, ..) i could be off one.
    either way, i decided to fix it up and give you all the update.
    // do class, main function stuff
         if(binary/1000 == 1){
              thousands += (binary/1000);
         temp = binary % 1000;          
         if(temp/100 == 1){
              hundreds += (temp/100);
         temp = temp % 100;          
         if(temp/10 == 1){
              tens += (temp/10);
         temp = temp % 10;
         if(temp/1 == 1){
              ones += (temp/1);
         // calculate binary to decimal     
         decimal = (ones * 1) + (tens * 2) + (hundreds * 4) + (thousands * 8);
         // take result and convert to string for output
         result += "Decimal: " + decimal + "\n";
    // display in gui ?
    // end function and class stuff(note: i suppose the statements similar to "thousands += (binary/1000);" can be simplified as "thousands = 1;")

  • Checking and Converting binary, octal, decimal, and hex

    Hi,
    I have a classroom project which will need to validate that data entered in a text is of a certain type with a keyListener, and then convert that value to the other 3 types.
    I know character.isDigit will handle the decimal case, and that I can then use Integer.otString methods to convert to binary, octal, and hex, but what about the other cases?
    Thanks

    OK, this isn't working. If I've already established
    that the string entered into, for example, the
    integer-only textfield is a valid integer, I should be
    able to simply use integer.parseint(s, 2) to convert
    it into binary, right?Not exactly. You should be able to use Integer.parseInt(s, 2) to return the result of converting it from binary representation. You appear to think that this affects whatever "s" refers to, which is not the case. Note also, that method returns an int which is the decimal value in question.
    When you are thinking of int variables, be careful not to think of them as "decimal" or "binary". They are neither. They are just numbers. "Decimal" and "binary" are text representations of numbers, so those concepts can only be applied to strings.
    Integer.parseInt(s, 2);
    txtBin.setText(s);So here you want to assume that the input is a string that represents a number in decimal, and you want to find the string that represents the number in binary. Like this:// convert string in decimal representation to number
    int dec = Integer.parseInt(s);
    // convert int to binary representation as string:
    String binary = Integer.toBinaryString(dec);
    // write that to the text field
    txtBin.setText(binary);You could use a one-liner to do that, but you'll need the "dec" variable to set the other text boxes.
    Rembering why I hate OO...All of what I said there is true in just about all computer languages, OO or otherwise.
    PC&#178;

  • Converting Binary to Decimal

    I would like to convert 12 and 32 digit strings into decimal. Is there
    a function in labview that does this? Does anyone have a vi (or sub-vi)?
    I'm using version 5.1
    Thanks!
    -DG
    [email protected]
    Sent via Deja.com
    http://www.deja.com/

    Hi Vinny,
    Thanks for the posting Vinny (and Stu). I did figure it out. The
    problem I initially had was that I had a 2-D array going into a for
    loop. I can break the array into 1-D arrays and then change this into
    an array into a string. No problems.
    Thanks!
    -DG
    In article <[email protected]>,
    vjrecca wrote:
    > hmmm, now I don't understand the question, because scan from string
    with a
    > %b format surely does take a string like "101101101110" and convert
    it to
    > decimal number 2926. Could you give a specific example of the string
    and
    > the expected result
    > - Vinny Recca.
    >
    > [email protected] wrote:
    >
    > > I've tried that, but to no success (I first convert the array into a
    > > sprea
    d sheet string, and then use the scan from string function.
    > >
    > > I did write a for while loop to do the conversion (basically using a
    > > shift register and putting the converted bits into an array that's
    > > built be the shift register, then adding them all up at the end).
    > >
    > > The numbers look off. Does Labiew intrinsically change the bits
    like
    > > from 0's to 1's when it's handling them?
    > >
    > > Thanks.
    > >
    > > -DG
    > >
    > > In article <_g3g6.76071$[email protected]>,
    > > "Stu McFarlane" wrote:
    > > > use the %b format string in the scan from string function (string
    > > functions)
    > > >
    > > > wrote in message news:95qec3
    > > $ekn$[email protected]..
    > > > > I would like to convert 12 and 32 digit strings into decimal. Is
    > > there
    > > > > a function in labview that does this? Does anyone have a vi (or
    sub-
    > > vi)?
    > > > >
    > > > > I'm using version 5.1
    > > > >
    > > > > Thanks!
    > > > >
    > > > > -DG
    > > > >
    > > > >
    [email protected]
    > > > >
    > > > >
    > > > > Sent via Deja.com
    > > > > http://www.deja.com/
    > > >
    > > >
    > >
    > > Sent via Deja.com
    > > http://www.deja.com/
    >
    >
    Sent via Deja.com
    http://www.deja.com/

  • Required code to convert binary to decimal

    i need the programming logic to convert a binary number into its decimal equivalent. the program should also detect a non binary number.

    public class EnterBinary{
       public static void main (String []args){
       int binCheck=0;
       String bins="";
       try {
          bins=args[0];
       catch (Exception e){
          System.out.print("No user input, program will terminate");
          System.exit(0);
       for(int i=0; i<bins.length(); i++)
          if((bins.charAt(i)=='1')||(bins.charAt(i)=='0')) binCheck++;
       if(binCheck==bins.length() ) {
          int j=1;
          binCheck=0;
          for(int i=0; i<bins.length()-1; i++) j *=2;
          for(int i=0; i<bins.length(); i++){
             if(bins.charAt(i)=='1') binCheck += j;
             j /=2;
       System.out.print("The binay number "+bins+" in decimal is "+binCheck);
       else System.out.print("Invalid binary input, program will terminate");
    }

  • Program to convert binary to decimal number?

    I only know how to do a program that converts only 4 numbers, but it to convert any amount of digits the number enters, I think you need to use a loop or something, but I'm not sure how. Could someone please help me? This is my code so far:
    import javax.swing.JOptionPane;
    public class bintodec {
         public static void main (String[] args) {
              String input;
              int number,digit1,digit2,digit3,digit4,result;
              input = JOptionPane.showInputDialog ("Enter a binary number.");
              number = Integer.parseInt(input);
              digit1 = ((number % 10000) - (number % 10000 % 1000)) / 1000;
              digit2 = ((number % 1000) - (number % 1000 % 100)) / 100;
              digit3 = ((number % 100) - (number % 100 % 10)) / 10;
              digit4 = (number % 10);
              result = (digit1 * 8) + (digit2 * 4) + (digit3 * 2) + (digit4 * 1);
              System.out.println ( "Binary number: " + input + "\nConverted Decimal Number: " + result);
              System.exit( 0 );
         } // main
    } // bintodecany help is super-appreciated.

    I am not sure if this will help you. Check it out, run it and let me know if it helped.
    import java.util.*;
       public class BitwiseTest1 {
           public static void main(String[] args){
            System.out.println("Enter a integer number:");
            Scanner keyboard = new Scanner(System.in);
            int n1 = keyboard.nextInt();
            System.out.println( "n1 as binary " +
                   Integer.toBinaryString( n1 ));
            System.out.println( "NOT n1 =     " +
                   Integer.toBinaryString( ~n1 ));
            System.out.println("Enter another integer number:");
            int n2 = keyboard.nextInt();
            System.out.println( "n2 as binary " +
                   Integer.toBinaryString( n2 ));
             System.out.println( "NOT n2 =     " +
                   Integer.toBinaryString( ~n2 ));
    }

  • Convert binary data to decimal in CAN XNET

    I am using NI-XNET Read function to read CAN frames from port CAN1 , the data out put comes in cluster in binary format is there any function that I can use to convert the binary to decimal ? I prefer not to write another sub-vi to do conversion but using the existing functions that can use my .dbc file and parse all frames in decimal format.
    Thanks.

    Yes Doug You are correct. We have a PCB and we have connected two inductive  sensors over the PCB which are measuring the distance and transpose it to voltage. The PCB has a software which transpose this voltage through an ADC and through the Can Bus we are reading the sensors .To communicate with the PCB  we are using a USB to Can device from IXXAT to communicate, we have build the Labview drivers and our problem as you mention is that we want to transpose this data to voltage. How we can do this? I try several times to upload photos or even the program we have create but for some reason the site is not letting me to do that. If you have an email i can send you the program we have create to tell us your opinion in this problem we have, we can also send you the Labview drivers and the datasheet of the sensor we are using.
    Attachments:
    Read from Can.vi ‏37 KB

  • Sample code converting binary data to image file

    Hi experts ,
    I need sample code to convert binary data (bytes) in to an image file.
    any help will be appreciated.
    Thanks and Regards,
    Naresh

    You need to show binary and decimal?  Or now just decimal?
    If binary and decimal, you can right click on your indicator and choose "Display Format...".  If you select the Advanced Editing Mode, you can make soft interesting display formats.  This includes showing the same value in mulitple ways in the indicator.  Try something like "%032b - %d" for the format string.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Binary to Decimal problems

    Ok to give a background to why i am writing this and so everyone has a better understanding of exactly what it is that i want it to do it goes like this.... My girlfriend got a message from a friend that was all in ones and zeros, and no its not because something on one of the mail servers is messed up he meant for it to be that way. But through the process of converting the 8 digit binary numbers into decimal values so that the corresponding ANSI character could be found I decided that it was taking to damn long so i would just write a prog to do it for me and then i could send back an annoying long message to that guy with ease. Only there is a catch over the summer i went from hotshot for a newb to a newb without a clue, i more or less didnt write a single line of code over 4 months and now i am having problems.
    I wanted to write a program that could-
    -convert long strings of chars into all 8 digit binary code
    -as well as convert large blocks of 8 digit binary back into ANSI letter values and then ANSI characters
    -I want it to use a frame and have a relativly simple usage procedure
    Now that you know what i set out to do I can show you how far i got -
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.GradientPaint.*;
    public class BinaryToDec extends JFrame implements ActionListener
         // member variables
         JButton quit;
         JButton color;
         JButton convert;
         JLabel     label11_5, labelPassingWord;
         JTextField inputBinary, outputDecimal;
         Color      colorStore;
         String drawString;
         String bin;
         public BinaryToDec()
              drawString = "";
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setLocation(100,250);
              setTitle("Binary to Decimal");
              setSize(420,200);
              getContentPane().setBackground(Color.BLACK);
              getContentPane().setLayout(null);
              quit= new JButton("DONE");
              quit.setForeground(Color.GREEN);
              quit.setBackground(Color.BLACK);
              quit.setBounds(100,10,75,20);
              getContentPane().add(quit);
              quit.addActionListener(this);
              color= new JButton("Bkgnd Clr");
              color.setForeground(Color.GREEN);
              color.setBackground(Color.BLACK);
              color.setBounds(15,10,75,20);
              getContentPane().add(color);
              color.addActionListener(this);
              convert= new JButton("Convert to Decimal");
              convert.setForeground(Color.GREEN);
              convert.setBackground(Color.BLACK);
              convert.setBounds(185,10,75,20);
              getContentPane().add(convert);
              convert.addActionListener(this);
              label11_5 = new JLabel("Enter a Binary Number");
              label11_5.setBounds(75,60,250,20);
              label11_5.setForeground(Color.GREEN);
              getContentPane().add(label11_5);
              inputBinary = new JTextField(25);
              inputBinary.setBounds(5,40,225,20);
              inputBinary.setForeground(Color.BLACK);
              getContentPane().add(inputBinary);
              inputBinary.addActionListener(this);
              labelPassingWord = new JLabel("Your Deciaml Equivilant");
              labelPassingWord.setBounds(75,120,250,20);
              labelPassingWord.setForeground(Color.GREEN);
              getContentPane().add(labelPassingWord);
              outputDecimal = new JTextField(25);
              outputDecimal.setBounds(5,100,225,20);
              outputDecimal.setForeground(Color.BLACK);
              getContentPane().add(outputDecimal);
              outputDecimal.addActionListener(this);
              setVisible(true);
         }// end of constructor
         public void actionPerformed (ActionEvent evt)
              if(evt.getActionCommand().equals("DONE"))
                   System.out.println("Quit Caught");
                   int choice = JOptionPane.showConfirmDialog(null , "You Pressed " + evt.getActionCommand() + " are you sure?"
                        , "Close Frame",JOptionPane.YES_NO_OPTION);
                   if(choice == 0)
                        this.dispose();
                   if (evt.getSource() == color)
                        Color color = Color.lightGray;
                        color = JColorChooser.showDialog(BinaryToDec.this,"Choose a color",color);
                        getContentPane().setBackground(color);
                   if (evt.getSource() == convert)
                        bin = inputBinary.getText();
                        int strLength = bin.length();
                        int pos1=0, pos2=7;
                        String temp = "";
                        String Decimal1 = "";
                        String Decimal2 = "";
                        int binLength = 8;
                        int count = 0;
                             do
                                  temp = bin.substring(pos1,pos2);
                                  Decimal1 = Convert(temp);
                                  Decimal2 = Decimal2 + "" + Decimal1;
                                  pos1 = pos1 + binLength;
                                  pos2 = pos2 + binLength;
                                  count++;
                                  outputDecimal.setText(Decimal2);
                                  repaint();
                             }while((strLength/8) >count);
         }//end of actionPerform
         public void paint( Graphics g)
              super.paint(g);
         public String Convert(String a)
              int ctr=1, decimal=0;
              for (int i = a.length()-1; i>=0; i--)
              if(a.charAt(i)=='1')      decimal+=ctr;
              ctr*=2;
              String converted = "" + decimal;
              return converted;
         public static void main(String args[])
              System.out.println("Binary to Dec");
              BinaryToDec in = new BinaryToDec();
         }// end of main()
    }// end of class FrameExample1
    OK so other than that i decided to play with some of the paint functions i more or less stayed on task, at first i had it just convert 8 digit binary nums into decimal form using this -
              int ctr=1, decimal=0;
              for (int i = a.length()-1; i>=0; i--)
              if(a.charAt(i)=='1')      decimal+=ctr;
              ctr*=2;
    which i found in someone elses post and made some changes to, thank you someone whoever you are i hope you see this, anyways,.. then i decided i wanted it to check the length of a string and divide it by 8 assuming that it it a perfect block of 8 digit nums and then cut it into substring and go through the proccess of converting the binary to deciaml one piece at a time. I did it more or less, well it compiles right but whenever you put in more than one 8 digit binary number it ends up giving you some really strange decimal numbers and i cant seem to figure out why the hell it is doing this.??? I was hoping that some one could help me out.
    If you read this far than thanks for taking the time on my post :)

    Did you try to decode that message by hand and verified that it really is encoded the way you think it is?
    Here some code to try
    public static void decode(String fileIn, String fileOut) {
            try {
            FileReader in=new FileReader(fileIn);
            PrintWriter out=new PrintWriter(fileOut);
            char[] buffer=new char[8];
            int i;
            do {
                i=in.read(buffer, 0, 8);
                if(i==8) {
                    out.write(Integer.parseInt(new String(buffer),2));
            } while(i==8);
            in.close();
            out.close();
            } catch (Exception e) {}
        public static void encode(String fileIn, String fileOut) {
            try {
            FileReader in=new FileReader(fileIn);
            PrintWriter out=new PrintWriter(fileOut);       
            int i;
            while( (i=in.read()) >= 0 ) {
                String str=Integer.toBinaryString(i);
                for(int j=str.length(); j<8; j++)
                    out.write('0');
                out.write(str);
            in.close();
            out.close();
            } catch (Exception e) {}
        }

  • NI 9870 and binary coded decimal (BCD)

    I have a scale that outputs the weight in 4 digit ASCII form which I would like to input into a CompactRio 9072 with a NI 9870 card and convert to a binary integer. The weight appears as the 7th to 10th character on lines that are terminated by a CR. I am thinking I will need a loop that once the 10th character is read, the 4 least significant bits of the 4 characters are converted from BCD to binary and the character counter resets to 0 when a CR is read. I need a good method for converting BCD to an integer value in either labview or vhdl. The rest of the logic for this project is in vhdl, so I would not mind adding the conversion logic there, except that all the algorithms I have found require decimal division or comparison in several clock cycles.
    Solved!
    Go to Solution.

    Hello imr
    Thank you very much for getting in touch with us!
    I believe the following community example will resolve part of your issue.
    Binary String to Number
    https://decibel.ni.com/content/docs/DOC-11704
    I have also provided some further information for those unaware of what Binary Coded Decimal represents.
    What is Binary Coded Decimal (BCD)?
    http://digital.ni.com/public.nsf/allkb/59C2A05C123​008F286256CA90069A6F8
    Thank you for choosing National Instruments!
    Sincerely,
    Greg S.

  • Binary to decimal, including negatives

    HI i have been searching through the forums but have been unable to find how to do this.
    I am using a 12bit word and need to convert it to decimal. I have tried using the Integer.parseInt(b, 2) method, however i cannot seam to make this work for negative numbers. I am using the two's compliment method to store negative numbers, for example -3 becomes 111111111100.
    Thanks for any help that you can give me!
    Alex

    The reason i'm using a 12 bit word is that the program is a simulator for the first minicomputer the DEC PDP-8. What i have to make you see is a machine code simulator, that reads in decimal values from a text file then converts these to binary and uses them as machine code.
    There seams to be an awefull lot of decimal to binary and visa versa conversion in this as when i perform a simple add the easiest way i have found is to convert both numbers to dec then add then convert back to binary.
    Thanks for all the help your giving me i still havn't managed to make it work yet though!
    Alex

  • Help with binary to decimal, binary to hex, and hex to ascii or ascii to hex program

    I decided to do a program that will do binary to decimal, binary to hex, and hex to ascii for a project related to a java programming course, which only needs to perform features from chapters 1-6 and 8 of Tony Gaddis's book. The functions work fine as their own main programs out side of this combined effort, so can anyone help me determine why I get the following 41 errrors saying: class, interface, or enum expected as well as any other errors that may show up afterwards because I'm stumped. My flowcharts, which have to be revised after discovering that my previous function were logically incorrect after running them in their own main are attached below as the spec sheet.
    My code is as follows and I hope you don't mind the commented lines of unused code because I'm not sure where I want things and what I want at the moment:
    import java.util.Scanner;
    import java.io.*;
    import java.lang.*;
    public class BintoDectoHextoAscii
       public static void main(String[] args)throws IOException
          Scanner input = new Scanner(System.in);
          System.out.println("Enter a binary number: ");
          String binary = input.nextLine(); // store input from user
         if (binary == input.nextLine())
          //int i= Integer.parseInt(hex,2);
          //String hexString = Integer.toHexString(i);
          //System.out.println("Hexa decimal: " + hexString);
          //int finaldecimalvalue = binaryToDecimal(hexString);
          int finaldecimalvalue = binaryToDecimal(hexString);
         if (binary != input.nextLine())
          String hexInput; // The variable Bin Input declared as the datatype int to store the Binary value  
          // Create a Scanner object for keyboard input.
          //Scanner keyboard = new Scanner(System.in);
          // Get the number of binary files.
          System.out.print("Enter the Hex value: ");
          hexInput = keyboard.nextLine();
          System.out.println("Original String: "+ hexInput);
          //String hexEquivalent = asciiToHex(demoString);
          String hexEquivalent = asciiToHex(hexInput);
          //Hex value of original String
          System.out.println("Hex String: "+ hexEquivalent);
          String asciiEquivalent = hexToASCII(hexEquivalent);
          //ASCII value obtained from Hex value
          System.out.println("Ascii String: "+ asciiEquivalent);String finalhexOutput = HextoAsciiConverter(hexEquivalent);
         if (binary != input.nextLine() && hexInput != keyboard.nextLine())
             BufferedReader binInput = new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter the Binary number:");
             String hex = binInput.readLine();
             //String finaldecimalvalue = binaryToDecimal(decimal);
             //long finalhexvalue = BinaryToHexadecimal(num);
             long finalhexvalue = BinaryToHexadecimal();
       public static String BinaryToHexadecimal(String hex)
          //public static void main(String[] args)throws IOException
             //BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
             //System.out.println("Enter the Binary number:");
             //String hex = binInput.readLine();
             long num = Long.parseLong(hex);
             long rem;
             while(num > 0)
             rem = num % 10;
             num = num / 10;
             if(rem != 0 && rem != 1)
             System.out.println("This is not a binary number.");
             System.out.println("Please try once again.");
             System.exit(0);
             int i= Integer.parseInt(hex,2);
             String hexString = Integer.toHexString(i);
             System.out.println("Hexa decimal: " + hexString);
          return num.tolong();
      //int i= Integer.parseInt(hex,2);
      //String hexString = Integer.toHexString(i);
      //System.out.println("Hexa decimal: " + hexString);
    //} // end BintoDectoHextoAsciil
       //public static String HexAsciiConverter(String hextInput)
          // Get the number of binary files.
          //System.out.print("Enter the Hex value: ");
          //hexInput = keyboard.nextLine();
          //System.out.println("Original String: "+ hexInput);
          //String hexEquivalent = asciiToHex(demoString);
          //String hexEquivalent = asciiToHex(hexInput);
          //Hex value of original String
          //System.out.println("Hex String: "+ hexEquivalent);
          //String asciiEquivalent = hexToASCII(hexEquivalent);
          //ASCII value obtained from Hex value
          //System.out.println("Ascii String: "+ asciiEquivalent);
       //} // End function  
       private static String asciiToHex(String asciiValue)
          char[] chars = asciiValue.toCharArray();
          StringBuffer hex = new StringBuffer();
          for (int i = 0; i < chars.length; i++)
             hex.append(Integer.toHexString((int) chars[i]));
          return hex.toString();
       private static String hexToASCII(String hexValue)
          StringBuilder output = new StringBuilder("");
          for (int i = 0; i < hexValue.length(); i += 2)
             String str = hexValue.substring(i, i + 2);
             output.append((char) Integer.parseInt(str, 16));
          return output.toString();
       public static String binaryToDecimal(String binary)
            //Scanner input = new Scanner(System.in);
            //System.out.println("Enter a binary number: ");
            //String binary = input.nextLine(); // store input from user
            int[] powers = new int[16]; // contains powers of 2
            int powersIndex = 0; // keep track of the index
            int decimal = 0; // will contain decimals
            boolean isCorrect = true; // flag if incorrect input
           // populate the powers array with powers of 2
            for(int i = 0; i < powers.length; i++)
                powers[i] = (int) Math.pow(2, i);
            for(int i = binary.length() - 1; i >= 0; i--)
                // if 1 add to decimal to calculate
                if(binary.charAt(i) == '1')
                    decimal = decimal + powers[powersIndex]; // calc the decimal
                else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
                    isCorrect = false; // flag the wrong input
                    break; // break from loop due to wrong input
                } // end else if
                // keeps track of which power we are on
                powersIndex++; // counts from zero up to combat the loop counting down to zero
            } // end for
            if(isCorrect) // print decimal output
                System.out.println(binary + " converted to base 10 is: " + decimal);
            else // print incorrect input message
                System.out.println("Wrong input! It is binary... 0 and 1's like.....!");
            return decimal.toint();
       } // end function
    The errors are as follows:
    ----jGRASP exec: javac BintoDectoHextoAscii.java
    BintoDectoHextoAscii.java:65: error: class, interface, or enum expected
       public static String BinaryToHexadecimal(String hex)
                     ^
    BintoDectoHextoAscii.java:73: error: class, interface, or enum expected
             long rem;
             ^
    BintoDectoHextoAscii.java:74: error: class, interface, or enum expected
             while(num > 0)
             ^
    BintoDectoHextoAscii.java:77: error: class, interface, or enum expected
             num = num / 10;
             ^
    BintoDectoHextoAscii.java:78: error: class, interface, or enum expected
             if(rem != 0 && rem != 1)
             ^
    BintoDectoHextoAscii.java:81: error: class, interface, or enum expected
             System.out.println("Please try once again.");
             ^
    BintoDectoHextoAscii.java:83: error: class, interface, or enum expected
             System.exit(0);
             ^
    BintoDectoHextoAscii.java:84: error: class, interface, or enum expected
             ^
    BintoDectoHextoAscii.java:87: error: class, interface, or enum expected
             String hexString = Integer.toHexString(i);
             ^
    BintoDectoHextoAscii.java:88: error: class, interface, or enum expected
             System.out.println("Hexa decimal: " + hexString);
             ^
    BintoDectoHextoAscii.java:90: error: class, interface, or enum expected
          return num.tolong();
          ^
    BintoDectoHextoAscii.java:91: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:124: error: class, interface, or enum expected
          StringBuffer hex = new StringBuffer();
          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
                          ^
    BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
          for (int i = 0; i < chars.length; i++)
                                            ^
    BintoDectoHextoAscii.java:128: error: class, interface, or enum expected
          ^
    BintoDectoHextoAscii.java:130: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
          ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
                          ^
    BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
          for (int i = 0; i < hexValue.length(); i += 2)
                                                 ^
    BintoDectoHextoAscii.java:138: error: class, interface, or enum expected
             output.append((char) Integer.parseInt(str, 16));
             ^
    BintoDectoHextoAscii.java:139: error: class, interface, or enum expected
          ^
    BintoDectoHextoAscii.java:141: error: class, interface, or enum expected
       ^
    BintoDectoHextoAscii.java:144: error: class, interface, or enum expected
       public static String binaryToDecimal(String binary)
                     ^
    BintoDectoHextoAscii.java:150: error: class, interface, or enum expected
            int powersIndex = 0; // keep track of the index
            ^
    BintoDectoHextoAscii.java:151: error: class, interface, or enum expected
            int decimal = 0; // will contain decimals
            ^
    BintoDectoHextoAscii.java:152: error: class, interface, or enum expected
            boolean isCorrect = true; // flag if incorrect input
            ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
            ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
                           ^
    BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
            for(int i = 0; i < powers.length; i++)
                                              ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
            ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
                                             ^
    BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
            for(int i = binary.length() - 1; i >= 0; i--)
                                                     ^
    BintoDectoHextoAscii.java:166: error: class, interface, or enum expected
                else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
                ^
    BintoDectoHextoAscii.java:169: error: class, interface, or enum expected
                    break; // break from loop due to wrong input
                    ^
    BintoDectoHextoAscii.java:170: error: class, interface, or enum expected
                } // end else if
                ^
    BintoDectoHextoAscii.java:174: error: class, interface, or enum expected
            } // end for
            ^
    BintoDectoHextoAscii.java:180: error: class, interface, or enum expected
            else // print incorrect input message
            ^
    BintoDectoHextoAscii.java:185: error: class, interface, or enum expected
            return decimal.toint();
            ^
    BintoDectoHextoAscii.java:186: error: class, interface, or enum expected
       } // end function
       ^
    41 errors
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    so can anyone help me determine why I get the following 41 errrors saying: class, interface, or enum expected as well as any other errors that may show up afterwards because I'm stumped.
    Yes - YOU CAN!
    My code is as follows and I hope you don't mind the commented lines of unused code because I'm not sure where I want things and what I want at the moment:
    Excellent! Commenting out code is EXACTLY how you troubleshoot problems like yours.
    Comment out sections of code until the problem goes away. Then start adding back ONE SECTION of code at a time until the problem occurs. When it does you have just FOUND the problem.
    If you do that you wind up with code that looks like this:
    import java.util.Scanner;
    import java.io.*;
    import java.lang.*;
    public class BintoDectoHextoAscii  {
          public static void main(String[] args)throws IOException      {
             Scanner input = new Scanner(System.in);
             System.out.println("Enter a binary number: ");
             String binary = input.nextLine(); // store input from user
                  public static String BinaryToHexadecimal(String hex)     {     } // end function        
    Notice ANYTHING UNUSUAL?
    You have a complete CLASS definition followed by a method definition.
    Methods have to be INSIDE the class - you can NOT define methods on their own.
    Write modular code.
    Write EMPTY methods - just the method signature and maybe a RETURN NULL if you need to return something.
    Then add calls to those empty methods.
    When everything compiles and runs find you can start adding code to the methods ONE METHOD AT A TIME.
    Test compile and run after you add the code for each method.

  • Converting Binary Data to Decimals

    Hello, my task is to use the VI I created which reads magnetic card strips and displays the 32 bit X 8 binary code. I need to display the binary code as decimals. Here is my VI, where do I go from here?
    Fernando
    Attachments:
    1MCardReader_finalCopy.vi ‏55 KB

    You need to show binary and decimal?  Or now just decimal?
    If binary and decimal, you can right click on your indicator and choose "Display Format...".  If you select the Advanced Editing Mode, you can make soft interesting display formats.  This includes showing the same value in mulitple ways in the indicator.  Try something like "%032b - %d" for the format string.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Converting binary data to cluster

    Hello,
    I have a problem converting binary data to a cluster. I have to create a VI that reads out some parameter from a device, which is connected via RS232. I get an array of 128 Byte from the device. The data has the following (C-)structure:
    struct PARAM
    char cVersion[64];
    float fDeviceID;
    float fChannels;
    float fSensor;
    float fTrigger;
    float fConst[5];
    char cReserved[28];
    How can I convert the binary array to a cluster? I created a cluster (see attachment), but I couldn't connect it with the "Type Cast" VI. Has anyone an idea how to solve this problem?
    Thanks, Martin
    Attachments:
    cluster.jpg ‏16 KB

    It is tedious to convert binary data to LabVIEW data when you have arrays. You have to typecast the data with clusters of the same fixed length as in the structure. Large clusters are easily created on the diagram using Array to Cluster primitive. See the attavhed picture.
    LabVIEW, C'est LabVIEW
    Attachments:
    conversion.gif ‏11 KB

  • Convert Binary Data into Pdf & send it as attachment in a mail in Workflow

    Hi,
    Scenario:
    The interactive form saved in WebDynpro Application is sent to R/3 in binary format. It has to be converted into pdf and sent it as an attachment in mail to the respective person in workflow.
    Kindly help on these issues :
    1. How to receive the binary data in R/3 sent by the WebDynpro Application ?
    To my knowledge we can receive the binary in XSTRING data type. Plz correct me if am wrong.
    2. How do i convert the received binary data into pdf ?
    Thanks,
    Bharath Kaushik

    Hi Bharath,
    I think you should try to write dat being sent to R/3 to spool first, as in R/3 there is FM <i>CONVERT_ABAPSPOOLJOB_2_PDF</i> , with the help of which you will be able to convert Binary data to PDF format.
    Pls find one of the threads related to this , and see if this is useful to you.
    Problem in CONVERT_ABAPSPOOLJOB_2_PDF.
    Hope this atleast helps to start off.
    Regds,
    Akshay Bhagwat
    PS: Some points would be nice if it helps:)

Maybe you are looking for

  • Custom Report Giving Exception

    While calling my custom report from webconsole i am getting these exceptions Class/Method: tcReportOperationsBean/getPagedReportData encounter some problems: {1} Caused by [Nested Exception]: java.sql.SQLException: Missing IN or OUT parameter at inde

  • Unable to Run Form more than once!

    Hi, 1. I create an oracle 10g Form 2. I am trying to "Run Form" from within the Forms Builder. It runs OK. 3. I close the IE, and try to "Run Form" again. It fails! My problem is that my form runs only the first time!!! However, If i try to run it ma

  • Blod Brush Tool Pressure Option is Dissabled, why?

    Hey, when I try to apply the pressure option for the blod brush tool it appears as dissbaled, watermarked; as well as the further options. I can only use the fixed and random option for each size, angle, and roundness. Can anyone help me through, ple

  • Which is better when charging? Turning it off or leaving it on?

    Which is better when charging? Turning it off or leaving it on?

  • Error in login in business one...

    Hello! I'm starting to work in B1, and today i'm not entering in the system with the only user that was created.... msg 131-93 How can i enter now to create or change users? Thanks for help!! Greetings