Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to convert binary to Decimal part 1 Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Convert binary to Decimal part 1 19-Jun-05
Category
Algorithm
Language
JBuilder All Versions
Views
353
User Rating
7
# Votes
1
Replies
0
Publisher:
Harrington, Mark
Reference URL:
			1   
2   import java.io.*;
3   import java.awt.*;
4   import java.awt.event.*;
5   
6   /* Main Class for handling Decimal to binary conversation written by MD 
7    Harrington Friday, 17 June 2005 */
8   
9   
10  class IntegerToBinary 
11  
12  {
13  
14  private int num ;
15  
16                  public IntegerToBinary(int number)
17                  {
18                          try
19                          {
20                          this.setNumber(number) ;
21                          }
22                          catch(IntegerRangeException e)
23                          {
24                          System.out.println(e.toString());
25                          }
26                  }// end constructor
27  
28  
29  
30  
31  public void setNumber(int value)throws IntegerRangeException
32  {
33  
34  /* Maximum representation for 32-bit platform for java Integer is between 
35  -2147483648 and 2147483647  So if we exceed this we want to throw an exception
36   and catch it I will put something together later for writing your own 
37   exception handlers but for now just accept We need to catch exceptions when
38   dealing with numbers */
39  
40    try
41    {
42                  if(( value > 2147483647)||(value < -2147483648))
43                  {
44                  IntegerRangeException e = new IntegerRangeException
45                  ("Number must be > =  -2147483648 or  < = 2147483647 ");
46                  throw e ;
47  
48                  }
49                  else {
50                          num = value ;
51                  }
52    }
53          catch(IntegerRangeException IRE){ 
54  
55          System.out.println(IRE.toString());
56          }
57  
58  
59  }// end setNumber 
60  
61  
62  /* innner class for handeling excpetions when we exceed maximum permiseable 
63     integer representation */
64  
65  
66  public class IntegerRangeException extends Exception
67  {
68                                  public IntegerRangeException (String msg)
69                                  {
70                                          super(msg);
71                                  }
72  
73  };// end class IntegerRangeException 
74  
75  
76  public String getBits()
77  {
78          int mask = 1 << 31; // start with the most significant bit and move
79  // 31 places to the left String buffer class holds our final binary 
80  // representation of the decimal we used 
81  
82          StringBuffer buf = new StringBuffer(35);
83          for (int c =1 ; c <= 32 ;c++ ) // since a 32 bit number we must  
84          {                           //therefore move 1 left 32 times
85                  if ((num & mask)== 0)
86                  {
87      // if the result of our logical addition is 0 then append 0 to the buffer 
88                          buf.append( '0') ;
89                  }
90                  else 
91                  {   
92                          // if its not 0 then append 1 
93                          buf.append('1');
94                  }
95  
96                  num <<= 1 ; // shift mask left by 1 remember when we left shift
97                             // 1 the carry register operates 
98  
99  
100 /* To make our display of the number more attractive and sensible 8  
101 therefore move 1 left 32 times bits hence 0 to 7 and thereafter
102 xxxxxx xxxxxxx xxxxxxx xxxxxxx * bits to a byte 32 bit number comprises 4 bytes 
103  we append a space */ 
104 
105                 if (c % 8 == 0)
106 
107                 {
108                         buf.append(' ');
109                 }
110 
111         } // end for 
112 
113 // finaly we return a String representation of whats in the buffer
114 // and pop off the stack Java is Stack Architecture !!!!! 
115 
116  return buf.toString();
117 
118 }
119 
120 }// end IntegerToBinary


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC