Converting binary digits to its decimal equivalent.

Is there any methods to do such convertion ??
Have been doing the hard way. :(

hi,
look at the
public static int parseInt(String s,
int radix)
throws NumberFormatException
method in the Integer class, there are examples in the api, here is what is shows:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
there is one showing 1100110, 2 returns 102, binary to integer.

Similar Messages

  • 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");
    }

  • HELP! Converting ascii to its decimal codes

    Does anyone know how to convert ascii code to its decimal equivalent? Is there a simple function that can do this?
    Thanks for any help!!

    Yes there is.
    String whatever = "whatever"
    char[] chars = whatever.toCharArray();
    for(int i=0; i<chars.length; i++) {
    System.out.println( (int) chars[i] );
    And since its friday I wont flame you for not reading
    a simple tutorial, though its hard not to
    ehmm, the square brackets around the chars[i] are replaced by
    non-squares, or not visible, but they ARE square brackets

  • 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;")

  • 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 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);
    }

  • Convert between binary and signed/unsigned decimal numbers

    Hi all,
    Has anyone written a code to convert:
    binary number --> signed decimal
    binary number --> unsiged decimal
    signed decimal --> binary
    unsigned decimal --> binary
    Please help! I'm confusing.

    http://java.sun.com/j2se/1.4/docs/api/java/lang/Integer.html
    See the methods parseInt and toBinaryString.
    Please help! I'm confusing. You are, aren't you?
    (Sorry, couldn't resist...)

  • 15 int digits and 3 decimal digits number problem in CR for VS 2005

    Post Author: condeagustin
    CA Forum: Crystal Reports
    HiIm using the Crystal Reports that comes with VS 2005 and I'm having a problem showing a number with 15 int digits and 3 decimal digits. In my tests, for example I have a formula that only has this line of code:  356125478123456.251I want the report to show  356,125,478,123,456.251 but it shows  356,125,478,123,456.000Why does CR puts 000 when it has to be a 251!!! WHY?!!!! I have tried to put the number as a string and I have tried a lot of functions like Round, Truncate, ToText, CDbl, etc and it's always the same result. In the format object of that formula the decimals are 1.000 and the rounding is 0.001. And the following is the weirdest part:If, instead of 356125478123456.251 I put 56125478123456.251, CR shows 56,125,478,123,456.300If then I put  6125478123456.251 CR shows 6,125,478,123,456.250And then if I put 125478123456.251 CR SUCCESFULLY shows 125,478,123,456.251 That is the way I want it but with 15 int digits. It looks like CR only works with a maximum of 12 int digits, more than that, the decimals don't work. How can I fix this? Please help me!Kind regards,Agustín Conde 

    Post Author: Ken Wong
    CA Forum: Crystal Reports
    Hi Agustin,
    Crystal Reports uses signed doubles as its datatype for numbers.  A signed double on a 32 bit machine is represented by 8 bytes.  1 sign bit, 11 exponent bit, and 52 bit for the mantissa. This roughly corresponds to 15 digits of total precision in decimal; ie. it does not matter where your dot is. You have 15 digits before and after the decimal point combined.
    This behavior is pretty standard.  You should see similar behavior in applications such as excel.
    Hope this helps,
    Ken

  • How to convert binary file to a particular format?

    Hi,
    I am having a requirement. I have in database various kinds of files stored as binary format. Its a sybase database. Now the files can be .pdf, or .doc also. But they are stored in binary format.
    I need to read the file from database.
    Now I can use jdbc to read the particular column value which contains the file in binary format.
    But tricky part is how to convert the binary file entry in a proper respective file format? So I have another column which basically has the value to tell the type of file. So I have ".doc" or ".pdf" as each entry of file...
    So please help me how can we do this using Java?
    THanks

    Hi,
    I am having a requirement. I have in database various
    kinds of files stored as binary format. Its a sybase
    database. Now the files can be .pdf, or .doc also.
    But they are stored in binary format.
    I need to read the file from database.
    Now I can use jdbc to read the particular column
    value which contains the file in binary format.
    But tricky part is how to convert the binary file
    entry in a proper respective file format? So I have
    another column which basically has the value to tell
    the type of file. So I have ".doc" or ".pdf" as each
    entry of file...
    So please help me how can we do this using Java?
    THanks

  • 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

  • Conversion of a binary data stream to decimal numbers

    Hi
    I am having great difficult working out how to convert my binary data stream to decimal numbers.
    The data I am reading back is in the format of a binary string, starting with the Most Significant Bit (MSB) of the first word, then the corresponding Least Significant Bit (LSB), where a word is two bytes long. A carriage return indicates message termination.  The return message starts with ‘bin,’ followed by the number of bytes requested. No delimiters are used to separate the data, but a carriage return is appended onto the end of the data.
    bin,<first word msb><first word lsb>...<last word lsb><CR>
    e.g. bin,$ro¬z1;@*...etc
    Does anybody know of any examaple vi that can help me convert this data from binary to decimal numbers?
    Many Thanks
    Ash

    Hi Ashley,
    after getting the string you can strip the first 4 characters. After this try a typecasting to array of U16. If the numbers are not correct, you can add a swap bytes operation to the resulting array.
    Message Edited by GerdW on 09-13-2006 02:46 PM
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome
    Attachments:
    Convert.png ‏2 KB

  • SQL Server uniqueidentifier cast to binary...what is equivalent in Oracle?

    Hi,
    I am having trouble understanding equivalent data types in my conversion from SQL Server to Oracle.
    Let's say I have this statement in SQL Server, where the string being "passed in" is a uniqueidentifier:
    select cast(substring('EFDA8997-63AA-4F33-96CF-29BFC2CE252E',8,1) as binary(1));
    The tool I've used to convert this to Oracle says the equivalent statement to this is (where the string being "passed in" is a char:
    select cast((substr('EFDA8997-63AA-4F33-96CF-29BFC2CE252E',8,1)) as raw(1)) from dual;
    However, I get different results when testing these two statements:
    SQL Server: 0x37
    Oracle: 07
    What is the correct what to convert this statement to Oracle?
    Thank you!
    Mimi

    I should add that this an n-tier application, where data is passed across multiple tiers, so the client UI has no connection to the database.
    I'm assuming this means that select..for update is not suitable, as that would require a connection to the database to be held open for a specific user in the middle-tier. Not suitable for a scalable, n-tier scenario.

  • Is it possible to convert a float to two decimal places

    Can anyone help me to convert a float to two decimal places
    float f=16;
    System.out.println(f);
    It will print 16.0
    But I want to get printed as 16.00

    "convert" no, as no conversion is needed, obviously. Format the output, yes. Read the API for String and its printf method.

  • Convert rounded number into two decimal places

    Hi All,
    I have a Number 156 stired in a column having number(10,2);
    but i want to update it into 156.00......
    May be simple,,,,but how?
    Edited by: 887268 on May 29, 2012 10:04 AM

    Hi,
    156
    156.00
    156.0000000000000 and
    00000156.0000 are all the same NUMBER. There's nothing to convert.
    If you want to display a NUMBER in a certain way (e.g., with 2 digits after the decimal point) then use TO_CHAR, or have your front end format the column. In SQL*Plus, for example, you can use the COLUMN command:
    COLUMN   sal     FORMAT  99999.99
    SELECT  ename
    ,       sal
    FROM    scott.emp
    ;Output:
    ENAME            SAL
    SMITH         800.00
    ALLEN        1600.00
    WARD         1250.00
    JONES        2975.00
    ...

  • Error in running a function to convert coordinates in degrees to decimal for EXCEL VBA

    For your information, I have 3 cross-posts regarding this question - and all I can said there is still no firm solution regarding this error.
    1) http://stackoverflow.com/questions/27634586/error-in-running-a-function-to-convert-coordinates-in-degrees-to-decimal-for-exc/27637367#27637367
    2) http://www.mrexcel.com/forum/excel-questions/826099-error-running-function-convert-coordinates-degrees-decimal-excel-visual-basic-applications.html#post4030377 
    3) http://www.excelguru.ca/forums/showthread.php?3909-Error-in-running-a-function-to-convert-coordinates-in-degrees-to-decimal-for-EXCEL-VB&p=16507#post16507
    and the story of the error is as below:
    Currently I am working on VBA excel to create a widget to verify coordinates whether it lies under the radius of ANOTHER predefined and pre-specified sets of coordinates.
    In the module, I want to convert the coordinates from degrees to decimal before doing the calculation - as the formula of the calculation only allow the decimal form of coordinates.
    However, each and every time I want to run the macros this error (Run-time error '5', invalid procedure call or argument) will appear. Then, the debug button will bring me to below line of coding:
    degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1))
    For your information, the full function is as below:
    Function Convert_Decimal(Degree_Deg As String) As Double
    'source: http://support.microsoft.com/kb/213449
    Dim degrees As Double
    Dim minutes As Double
    Dim seconds As Double
    Degree_Deg = Replace(Degree_Deg, "~", "°")
    degrees = Val(Left(Degree_Deg, InStr(1, Degree_Deg, "°") - 1))
    minutes = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "°") + 2, _
    InStr(1, Degree_Deg, "'") - InStr(1, Degree_Deg, "°") - 2)) / 60
    seconds = Val(Mid(Degree_Deg, InStr(1, Degree_Deg, "'") + _
    2, Len(Degree_Deg) - InStr(1, Degree_Deg, "'") - 2)) / 3600
    Convert_Decimal = degrees + minutes + seconds
    End Function
    Thank you.
    Your kind assistance and attention in this matter are highly appreciated.
    Regards,
    Nina.

    You didn't give an example of your input string but try the following
    Sub test()
    Dim s As String
    s = "180° 30' 30.5""""" ' double quote for seconds
    Debug.Print Deg2Dec(s) ' 180.508472222222
    End Sub
    Function Deg2Dec(sAngle As String) As Double
    Dim mid1 As Long
    Dim mid2 As Long
    Dim degrees As Long
    Dim minutes As Long
    Dim seconds As Double ' or Long if only integer seconds
    sAngle = Replace(sAngle, " ", "")
    mid1 = InStr(sAngle, "°")
    mid2 = InStr(sAngle, "'")
    degrees = CLng(Left$(sAngle, mid1 - 1))
    minutes = CLng(Mid$(sAngle, mid1 + 1, mid2 - mid1 - 1))
    seconds = Val(Mid$(sAngle, mid2 + 1, 10)) ' change 10 to 2 if only integer seconds
    Deg2Dec = degrees + minutes / 60 + seconds / 3600
    End Function
    As written the function assumes values for each of deg/min/sec are included with unit indicators as given. Adapt for your needs.
    In passing, for any work with trig functions you will probably need to convert the degrees to radians.

Maybe you are looking for