2011-11-06 38 views
0

數組二進制輸出這裏是我的代碼:http://pastebin.com/x8XKSufP格式化從Java中

我需要的輸出格式轉換成適當的二進制格式。例如,如果你輸入一個小數點「64」,我需要它輸出「0100 0000」,而不是1000000.我一直試圖弄清楚這個問題一個多小時。請幫忙。

Test.java

import java.util.Scanner; 

public class Test { 
    public static int[] getBinaryArray(int decimal) { 
     int result = decimal; 
     int length = 0; 
     while (result != 0) { 
     result /= 2; 
     length++; 
     } 
     return new int[length]; 
    } // close getBinaryArray method 

    public static int[] decimalToBinary(int[] binary, int decimal) { 
     int result = decimal; 
     int arrayLength = binary.length - 1; 
     while (result != 0) { 
     if (result % 2 == 0) { 
      binary[arrayLength] = 0; 
     } else { 
      binary[arrayLength] = 1; 
     } 
     arrayLength--; 
     result /= 2; 
     } // close WHILE 
     return binary; 
    } // close decimalToBinary method 

    // Main method 
    public static void main(String[] args) { 
     // initialize the input scanner 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter a number in decimal format: "); 
     int getDecimal = input.nextInt(); 
     int[] binary = decimalToBinary(getBinaryArray(getDecimal), getDecimal); 
     for (int i = 0; i < binary.length; i++) { 
     System.out.println(i + " " + binary[i]); 
     } // close FOR 
    } // main method 
} 
+0

如果你的長度不是8的倍數,在前面加上足夠的「0',直到它。 –

+0

而不是發佈鏈接到您的代碼,發佈代碼本身。由於這是你的第一個問題,所以我冒昧爲你做這件事。 –

回答

0

這是幾乎沒有Kal的解決方案,乾淨的,但它確實給你的每4位,你問之間的空間。

import java.util.Scanner; 

class decimalToBinaryConverter { 

    public static void main(String[] args) { 
     int groupsOf = 4; //Change this to 8 if you want bytes instead of half bytes. 
     Scanner input = new Scanner(System.in); 

     System.out.println("Enter a number in decimal format: "); 

     int myInt = input.nextInt(); //Max in put value is of course max integer value, 2,147,483,647. Does not work with negative values. 
     int bitCount = Integer.SIZE - Integer.numberOfLeadingZeros(myInt); //Determines at what position the leading bit is. 
     int zerosNeeded = groupsOf - (bitCount % groupsOf); //Determines how many 0s need to be added to the bit group to ensure the group is full. 
     StringBuilder tempSB = new StringBuilder(); 
     StringBuilder resultSB = new StringBuilder(); 

     while (zerosNeeded > 0 && zerosNeeded != groupsOf) { 
      tempSB.append(0); 
      zerosNeeded--; 
     } 

     tempSB.append(Integer.toBinaryString(myInt)); //At this point tempSB has the correct bits in the correct places, but with no spaces. 

     for (int i = 0; i < tempSB.length(); i++) { 
      if (i % groupsOf == 0) { 
       resultSB.append(" "); //Put a space in between each bit group. 
      } 
      resultSB.append(tempSB.charAt(i)); 
     } 
     System.out.println(resultSB.toString().trim()); //Trim the extra space off the front of resultSB and print it. 
    } 
} 
0
System.out.printf("%d%d%d%d %d%d%d%d", 
       (n&128)>0?1:0, 
       (n&64)>0?1:0, 
       (n&32)>0?1:0, 
       (n&16)>0?1:0, 
        (n&8)>0?1:0, 
        (n&4)>0?1:0, 
        (n&2)>0?1:0, 
        n&1) ; 

...等等。

0

轉換爲binaryString,然後離開填充零應該做的伎倆。

下面是一個簡單的例子(不考慮大的二進制字符串)

String s = Integer.toBinaryString(i); 

int length = s.length(); 
int remainder = length % 8; 
if (remainder != 0) 
    length = length + (8 - remainder); 

System.out.println(String.format("%0" + length + "d" , Integer.parseInt(s))); 
+0

這不會像OP想要的那樣在字節之間添加空格。 – ubiquibacon

0

我不知道你的代碼的「使用」,但這裏是我的解決方案(不要忘了導入的java .IO):

public static void main(String args[]) throws IOException { 

    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter the decimal value: "); 
    String input = bf.readLine(); 

    // Parse the input 
    int i = Integer.parseInt(input); 

    String bin = null; 
    String result = null; 

    if ((Integer.toBinaryString(i).length() % 2) != 0) { 
     bin = "0".concat(Integer.toBinaryString(i)); 
     // Formatting obtained binary 
     result = (bin.substring(0, bin.length()/2)).concat(" ").concat(
       bin.substring(bin.length()/2, bin.length())); 
    } else { 
     bin = Integer.toBinaryString(i); 
     //Formatting obtained binary 
     result = (bin.substring(0, bin.length()/2)).concat(" ").concat(
       bin.substring(bin.length()/2, bin.length())); 
    } 

    System.out.println("Binary: " + result); 
} 

享受:)