Advertisement
T99

NumberBaseConverter.java

T99
Jan 31st, 2018
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. /*
  2.  *  Copyright 2017, Trevor Sears <trevorsears.main@gmail.com>
  3.  *
  4.  *  Licensed under the Apache License, Version 2.0 (the "License");
  5.  *  you may not use this file except in compliance with the License.
  6.  *  You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *  Unless required by applicable law or agreed to in writing, software
  11.  *  distributed under the License is distributed on an "AS IS" BASIS,
  12.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *  See the License for the specific language governing permissions and
  14.  *  limitations under the License.
  15.  */
  16.  
  17. /*
  18.  * IMPORTANT -- THIS CLASS CURRENTLY USES A DEPRECATED CLASS 'BINARY' THAT ALLOWS BINARY INFORMATION TO BE STORED
  19.  * CHECK THE REPOSITORY FOR MORE RECENT CHANGES.
  20.  *
  21.  *  --> SHIT'S DEPRECATED YO (this class has been deprecated/overhauled as well)
  22.  */
  23.  
  24. import java.util.ArrayList;
  25.  
  26. public class NumberBaseConverter {
  27.    
  28.     public static Binary decToBin(int dec) {
  29.        
  30.         ArrayList<Boolean> binary = new ArrayList<>();
  31.         binary.add(true);
  32.         int factor = 0;
  33.         int i1 = 1;
  34.         boolean found = false;
  35.        
  36.         if (dec == 0) {
  37.            
  38.             return new Binary(new Boolean[] {false, false, false, false, false, false, false, false});
  39.            
  40.         }
  41.        
  42.         while (!found) {
  43.            
  44.             if (i1 * 2 > dec) {
  45.                
  46.                 factor = i1;
  47.                 found = !found;
  48.                
  49.             } else {
  50.                
  51.                 binary.add(false);
  52.                 i1 *= 2;
  53.                
  54.             }
  55.            
  56.         }
  57.        
  58.         dec -= factor;
  59.        
  60.         for (int i2 = new Double(Math.log(factor)/Math.log(2)).intValue(); i2 >= 0; i2--) {
  61.            
  62.             if (factor <= dec) {
  63.                
  64.                 dec -= factor;
  65.                 binary.set(binary.size() - (i2 + 1), true);
  66.                
  67.             }
  68.            
  69.             factor /= 2;
  70.            
  71.         }
  72.        
  73.         while (binary.size() % 8 != 0) {
  74.            
  75.             binary.add(0, false);
  76.            
  77.         }
  78.        
  79.         return new Binary(binary);
  80.        
  81.     }
  82.    
  83.     public static int binToDec(Binary bin) {
  84.        
  85.         int size = bin.size() - 1;
  86.         int dec = 0;
  87.        
  88.         for (int bit = 0; bit <= size; bit++) {
  89.            
  90.             if (bin.getBit(size - bit)) {
  91.                
  92.                 dec += Math.pow(2, bit);
  93.                
  94.             }
  95.            
  96.         }
  97.        
  98.         return dec;
  99.        
  100.     }
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement