Decimal to binary conversion

hi...anyone here knows the code to convert decimal to binary.Please help me!!!

Here's the answer in a riddle - to fill in the blank line is to understand the algorithm used - have fun!
class BinaryConverter{
   public static void main(String []args) throws Error{
      try{
         new BinaryConverter(Integer.parseInt(args[0]));
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("No argument entered");
      } catch(NumberFormatException e){
         System.out.println("Enter whole numbers only");
   private BinaryConverter(int i){
      System.out.println("The binary number is: ");          
      convert(i, 0, (long)Math.pow(2, 31));
   private void convert(int num, int i, long max){
      if(num / max==1) System.out.print("1");
      else System.out.print("0");
      num %= max;
      // there is a line deliberately removed here!
      if(i++ % 8 ==0) System.out.print(" ");
      if(max >0) convert(num, i, max);
}

Similar Messages

  • Decimal to binary conversion script

    hi
    i m very happy to all of u on ur response
    i m really happy with this forum
    i need a script to convert the decimal number in to binary
    number
    83---------->1010011
    thx

    here are two handlers I picked up from James Newton. I
    couldn't find them anywhere on his site (
    http://nonlinear.openspark.com)
    so I paste them directly here.
    credits go to James.
    on mMakeRuleMap(anInteger)
    -- INPUT: <anInteger> must be an integer
    -- OUTPUT: Returns a 32-item list of 0s and 1s corresponding
    to the
    -- binary representation of anInteger
    if anInteger < 0 then
    tBinary = mMakeRuleMap(the maxInteger + 1 + anInteger)
    tBinary[1] = 1
    else
    tBinary = []
    tBits = 32
    tBinary[32] = 0
    repeat while anInteger
    tBinary[tBits] = anInteger mod 2
    anInteger = anInteger / 2
    tBits = tBits - 1
    end repeat
    end if
    return tBinary
    end mMakeRuleMap
    on mBinaryToDecimal(aBitList)
    -- INPUT: <aBitList> is a list of 0s and 1s (up to 32
    items)
    -- OUTPUT: a decimal number corresponding to the binary
    number
    -- defined by <aBitList>
    tBitValue = 1
    tDecimal = 0
    i = aBitList.count
    if i = 32 then
    if aBitList[1] = 1 then
    -- The number is negative
    i = 31
    tDecimal = the maxInteger + 1
    end if
    end if
    repeat while i
    if aBitList
    then
    tDecimal = tDecimal + tBitValue
    end if
    tBitValue = tBitValue * 2
    i = i - 1
    end repeat
    return tDecimal
    end mBinaryToDecimal

  • Hi I would like to know if you can convert decimal to binary using actionscript 3.0?

    Hi I would like to know if you can convert decimal to binary using actionscript 3.0?

    Hi thanks for the response Ive tried to implement the code as you stated but it is not outputting please if you could check the following code and let me know where Ive gone wrong that would be much appreciated Thanks!.
    miles_txt.addEventListener(KeyboardEvent.KEY_DOWN,convertKilometres);
    miles_txt.restrict ="0-9";
    function convertKilometres (k:KeyboardEvent):void {
    var miles:Number;
    var kilometres:Number;
    if (k.keyCode == Keyboard.ENTER){
    miles=Number(miles_txt.text);
    kilometres=miles* 1.609344;
    kilometres_txt.text = kilometres.toPrecision(2);
    kilometreConvert_txt.addEventListener(KeyboardEvent.KEY_DOWN,convertKiloToMile);
    kilometreConvert_txt.restrict = "0-9";
    function convertKiloToMile(k:KeyboardEvent):void{
    var kilometreConvert:Number;
    var milesAnswer:Number;
    if (k.keyCode==Keyboard.ENTER) {
    kilometreConvert=Number(kilometreConvert_txt.text);
    milesAnswer=kilometreConvert* 0.621371192;
    milesAnswer_txt.text = milesAnswer.toPrecision(2);
    binary_txt.addEventListener(KeyboardEvent.KEY_DOWN, checkEnterKey2);
    function checkEnterKey2(e:KeyboardEvent):void{
    if(binary_txt.text != '' && e.keyCode == Keyboard.ENTER){
    decimal_txt.text=binaryToDecimal(binary_txt.text).toString();
    function binaryToDecimal(s:String):Number{
    var n:Number = 0
    for(var i:int=0;i<s.length;i++){
    n+=Number(s.substr(i,1))<<(s.length-1-i)
    return n;
    function decimalToBinary(n:Number):String{
    return n.toString(2);
    return_btn.addEventListener(MouseEvent.CLICK, goBackToCalculator);
    function goBackToCalculator(e:Event):void
    mybuttonSound.play();
    gotoAndStop("Calculator");
    clear_btn.addEventListener(MouseEvent.CLICK,clearField);
    function clearField(e:MouseEvent):void{
      mybuttonSound.play();
      miles_txt.text ="";
      kilometres_txt.text ="";
      milesAnswer_txt.text ="";
      kilometreConvert_txt.text ="";
      binary_txt.text ="";
      decimal_txt.text ="";

  • Binary Conversion of file in Emigall(why)

    Can any one tell me what is the purpose of converting the file to binary before loading through EMIGALL?Why is it necessary?I have checked the file On application server in the directory where file after binary conversion is placed.I was expecting it to be in binary format i mean combination of 0's and 1's.But it was not.So can anyone please elaborate.
    Thanks

    Hi Arya
    Very strange question.
    There's only one valid file format for the EMIGALL upload files at the moment, so obviously the file has to match that particular format.
    Not absolutely sure why we chose this file format instead of a delimited TXT file (it was a very long time ago), two things spring to mind:
    Inclusion of "special" characters in the content, that could also be used as column delimiter, without compromising the file layout
    Incorrect file layout, like missing or additional columns compared to the EMIGALL customising, easier to identify and more likely to fail
    Please note, I don't want to start a discussion here why one format is better than another. Simply accept it, it's not a big deal and move on.
    I am certain your file is in binary format, you only use the wrong editor to display the file
    Yep
    Jürgen

  • Do anyone know how to convert a decimal to binary, 1 into 01

    do anyone know how to convert a decimal to binary
    i wants to convert 1 into 01, but the computer print out 1, it didn't print 0
    also 0 it also print only 0, and 3 it print11
    so decimal of 3, the computer print correct, but 0 and 1 it print wrong
    how can i print 01 if my decimal is 1
    int a=1;
    System.out.print(Integer.toBinaryString(a));
    can anyone help me solve this question
    and
    you can use java or jsp
    if you can use jsp it would be better, if not use java

    it's not printing anything 'wrong'.
    0 in binary is 0
    1 in binary is 1
    All it's doing is cutting off leading zeros since they don't change the value.
    1 is the same as 01 is the same as 00000001...no difference
    Similarly, when you print decimal 3 as 11, it could just as easily say 00000011 and be correct, since the zeros don't change anything.

  • ASCII to binary conversion

    Does anyone knows how to convert the ASCII value to binary? For example
    ASCII 'U' to 01010101?

    Hi,
    If I understood you correctly, you want to convert a character eg:'U' to
    its ASCII value which is 85 (in binary representation 01010101).
    With the following method you can convert the first character of a string to
    a one byte long unsigned integer.
    method BaseClass.CharToInt(input pStr: Framework.string): Framework.ui1
    begin
    return *(pointer to ui1)(*(pointer to pointer)(&pStr));
    end method;
    See also the attached .pex file for some more, usefull little methods...
    Regards,
    Tamas Deak
    -----Message d'origine-----
    De: Sing Nyguk Ling [mailto:Thomas.Lingbass.com.my]
    Date: dimanche 12 mars 2000 09:49
    &Agrave;: kamranaminyahoo.com
    Objet: (forte-users) ASCII to binary conversion
    Does anyone knows how to convert the ASCII value to binary? For example
    ASCII 'U' to 01010101?
    For the archives, go to: http://lists.xpedior.com/forte-users and use
    the login: forte and the password: archive. To unsubscribe, send in a new
    email the word: 'Unsubscribe' to: forte-users-requestlists.xpedior.com

  • Conversion of Decimal to Binary String

    Hello Labview People:
    I'm rather new to Labview, so this may be a simple-minded question . . .
    I would like to take a numeric control and have the decimal input
    converted to a binary string of 8 bits. So, for example, if I were to
    put in the number '127', I would get out (on an indicator) the string:
    01111111
    The number '128' would show on the indicator as:
    10000000
    It seems to me that this should be simple, but somehow I cannot figure
    out how to do it. Must an array be built bit by bit to accomplish this?
    (I'm afraid I don't yet know how to build arrays.)
    Thanks in advance for any help you provide!
    Sent via Deja.com http://www.deja.com/
    Before you buy.

    Or, you can go under the numeric function, and look for conversions, get
    decimal to byte array conversion.
    Then what you need to do is to get an array selection vi, and index to zero.
    I don't remember the exact name of the VI's because I don't have LabView
    here with me.
    Perhaps this complicates things
    Steven
    "Timo Miettunen" wrote in message
    news:[email protected]..
    > use format into string VI from string palette
    >
    > format string should be "%08b" for 8 bits, "%016b" for 16 bits etc.
    >
    > timo
    >
    > Ananda Dave wrote:
    > >
    > > Hello Labview People:
    > >
    > > I'm rather new to Labview, so this may be a simple-minded question . . .
    > >
    > > I would like to take a numeric control and have the decimal input
    > >
    converted to a binary string of 8 bits. So, for example, if I were to
    > > put in the number '127', I would get out (on an indicator) the string:
    > >
    > > 01111111
    > >
    > > The number '128' would show on the indicator as:
    > >
    > > 10000000
    > >
    > > It seems to me that this should be simple, but somehow I cannot figure
    > > out how to do it. Must an array be built bit by bit to accomplish this?
    > > (I'm afraid I don't yet know how to build arrays.)
    > >
    > > Thanks in advance for any help you provide!
    > >
    > > Sent via Deja.com http://www.deja.com/
    > > Before you buy.
    >
    > --
    > -------------------------------------------------
    > Timo Miettunen
    >
    > Fincitec Oy Production Department
    > e-mail: [email protected]
    > Address:
    > Lumikontie 2 Tel: +358-16-2151245
    > PO BOX 11 Fax: +358-16-221561
    > FIN-94600 KEMI Finland
    > -------------------------------------------------

  • Conversion from decimal to binary!

    int i=1234;How can I convert an integer to its binary form??

    int i = 1234;
    String binInt = Integer.toBinaryString(i);

  • Decimal to Binary

    Hi,
    Can we convert decimal values stored in a column to binary using select statement?
    Has Oracle 10g any built-in function that supports such conversions.

    and of course an example using the model clause ;-)
    SQL> create table numbers
      2  as
      3  select 0 n from dual union all
      4  select 1 from dual union all
      5  select 2 from dual union all
      6  select 15 from dual union all
      7  select 16 from dual union all
      8  select 21 from dual union all
      9  select 1023 from dual union all
    10  select 1024 from dual union all
    11  select 3571 from dual
    12  /
    Tabel is aangemaakt.
    SQL> select a n
      2       , to_number(b) binary
      3    from numbers
      4   model
      5         partition by (n a)
      6         dimension by (0 i)
      7         measures (n, cast(null as varchar2(4000)) b)
      8         rules iterate(20)
      9         ( b[0] = b[0] || case when n[0] >= power(2,19-iteration_number) then '1' else '0' end
    10         , n[0] = n[0] - case when n[0] >= power(2,19-iteration_number) then power(2,19-iteration_number) else 0 end
    11         )
    12   order by n
    13  /
                       N               BINARY
                       0                    0
                       1                    1
                       2                   10
                      15                 1111
                      16                10000
                      21                10101
                    1023           1111111111
                    1024          10000000000
                    3571         110111110011
    9 rijen zijn geselecteerd.Regards,
    Rob.

  • Convert decimal to binary with a fixed length

    Hi, I need to write a program that converts a decimal number to binary with a fixed length (say 5), for example, decimal digit "0" -> binary number "00000". I try Integer.toBinaryString(), but it will just cut off the leading bits, so I wonder how to let the conversion happen, anyone can help me with that, thanks in advance.
    mckie

    Daiyue_Weng wrote:
    Hi, I need to write a program that converts a decimal number to binary with a fixed length (say 5), for example, decimal digit "0" -> binary number "00000". I try Integer.toBinaryString(), but it will just cut off the leading bits, so I wonder how to let the conversion happen, anyone can help me with that, thanks in advance.
    mckieWhat do you expect it to do? When you limit yourself to 5 digits binary you can only represent 0 to 63 and you will loose anything not in those 5 digits.

  • Binary Conversion of a File

    Hi Guys,
    The entire issue is on File Handling.
    There is one file say input.dat and another file say output.dat
    There are two classes Head and Tail which are Serializable. I am creating objects of those two classes.
    I am inserting the first object of Head, then first few bytes from input.dat and then the first object of Tail.
    Like this goes on till it reaches to the end of file. Till here everything is fine.
    Now the output.dat which is generating, I want to convert it into binary format (e.g. 10110)
    How can I go about it? After making it in a binary form, how can I retrive the data ?
    Please help me out
    Regards
    Unmesh

    1) the size of the byte array buffer is 4096. Why is it so ?It is an arbitrary value just plucked, almost, out of thin air. It can really be any value that you want, but, since computers a binary machines, a power of two is very desirable (in this case, 2^12). Secondly, in terms of buffer sizes, as the buffer size increases you eventually wind up with a diminishing returns issue (e.g., the larger the buffer, the more memory, more management, more time). To best see this, take a large file and start out with a buffer size of 1 byte and time how long it takes to process the entire file. Do this again but double the buffer size, now 2. It should take roughly half as long to process the entire file. Repeat this process until you see that the processing time comes close to hitting a plateau. Most implementations that utilize buffers tend to use 4K and 8K simply because it just seems to be the best payoff with the smallest price.
    2) i couldn't understand this line of the code
    for (int mask = 128; mask > 0; mask /= 2)
    From where are you getting these numbers 128 and 2 ? why are you dividing mask by 2 ?A byte consists of 8 bits (to be more precise, let us call this an octet). Each bit has a value - basic number system thing. So, for an octet, the bit values are (most significant bit first):
    7   | 6  | 5  | 4  | 3  | 2  | 1  | 0          bit
    128 | 64 | 32 | 16 | 8  | 4  | 2  | 1          decimal bit value
    80  | 40 | 20 | 10 | 08 | 04 | 02 | 01         hex bit valueTo liken this to the decimal, base 10, system, bit 0 is the 1's column, bit 1 is the 10's column, bit 2 is the 100's column and so on. The binary system (base 2) is mathematically the same. It is generalized like this for a given value at a given "column" for any number system:
    value := b^i
    where:
    b is the base number system in question
    i is the column in question
    So, to find the complete value of any number in any number system:
    value := db^i + db^i-1 + db^i-2 + ... + db^1 + db^0
    where:
    b is the base number system in question
    i is the column in question
    d is the value of the digit in the column in question
    I should also note that jummoMa1's approach of using bit shifting is more efficient.

  • Binary conversion of a big image leads to "java.lang.OutOfMemoryError"?

    Hi,
    My program loads a black and white 1 bit depth image of around 20 - 30mb. When I try to convert it to binary image, it gives the following error: java.lang.OutOfMemoryError: Java heap spaceI am converting a bufferedimage to binaryimage. I used the following conversion method:      
    // This method converts an image to a binary image:
         public static BufferedImage convert (BufferedImage src, int type)
              int w = src.getWidth ();
              int h = src.getHeight ();
              BufferedImage dst = new BufferedImage (w, h, type);
            Graphics2D g = dst.createGraphics ();
            g.drawRenderedImage (src, null);
            g.dispose ();
              return dst;
         }The program was working well before but now it keeps giving the heap memory error which is really weird. Is there any way I can fix this? Can anybody suggest a better way to convert a black and white image to binary data. Thanks.

    You could increase maximum memory (-Xmx): [http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html]

  • JMS Adapter binary conversion error

    I am getting an MQ message. The message has / embedded when it is picked by JMS Adapter. Because the message content has /, JMS is stripping the message. Is there anyway to get the whole message into XI from JMS without the message getting stripped.
    the error message is below
    Error while processing message '3c698d81-983c-4d3c-2272-cb505bbd3e4a';  detailed error description: com.sap.aii.adapter.jms.api.channel.filter.MessageFilterException: Error in converting Binary message 1193200ST20080509110023912134297100239120000DI2SAPDVDIROUTI7080509110023912 EDI00000        INITAREQDI2SAPDVBNSF           EBSTMNT 120886882PNCBANK        I N01                                      Y 01,043000096,FORTE,080422,0830,01,80,80,2/                                       02,RCVR,043000096,1,080422,0830,USD,/                                           03,1019283631,USD,030,28192752,,,060,28192752,,,072,000,,,074,000,,/
    Edited by: developer on Jul 1, 2008 9:55 PM

    hi folks i have resolved that issue it was mapping change...
    Now i'm facing another problem .. I m gettin only header record but i don see me items...
    my structure is like this field fixed length
    HEADER ( 1 )
    OrderDetail(Min - 1 and Max - Unbounded)
    Trailer(Min - 1 and Max - Unbounded)
    configuration of  my  JMS adapter :
    Transform.Class                              :com.sap.aii.messaging.adapter.Conversion
    Transform.ContentType                 :text/xml;charset=utf-8
    xml.conversionType                      :StructPlain2XML
    xml.processFieldNames                :fromConfiguration
    xml.documentName                     : MyDocument
    xml.documentNamespace           : http://xi.com/test
    xml.recordsetStructure               : Header,1,OrderDetail,,Trailer,
    xml.recordsetName                     : Dataset
    xml.Header.keyFieldName            :abc
    xml.Header.keyFieldValue            :x1
    XML xml.keyFieldType                 :CaseSensitiveString
    xml.Header.fieldFixedLengths     :1,2,3..
    xml.Header.fieldNames                :abc,cde,fgh...
    Similarly for order and trailer
    Kindly help in this regard.. i was not able to find in search

  • Converting from decimal to binary to hexadecimal to octal

    hello everyone,
    my question is, Is there a way to get decimal input from user and print the binary, hexadecimal and octal value of that decimal? just like C does in printf command

    http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html

  • Convert decimal to binary

    hi all!
    I want to convert from message box witch is in ascii (decimal) to a signal in binary... can anyone help me?
    thanks in advance

    hello,
    I've made this example. It's strange. But in the moment it's my only idea.
    it output's two different ways. Because I didn't understand exactly what you need.
    Software developer
    www.mcm-electronics.com
    PS: Don't forget to rate a good anwser ; )
    Currently using Labview 2011
    PORTUGAL
    Attachments:
    Convert.vi ‏10 KB

Maybe you are looking for