Java中怎么把一串二進制轉換成數組呢?
列入有000000111 ?
轉換成
000
000
111
我只寫到了將任意十進制轉換成二進制- - ?
求大神指點指點
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Conversation { ?
? ?public static void main(String args[]){
? ?Scanner input = new Scanner(System.in);
? ?System.out.print("請輸入二進制數字: ");
? ?int m=input.nextInt();
? ? ? ?toBinary(m);
? ?} ?
? ? static void toBinary(int num){ ?
? ? ? ?if(num/2==0){
? ? ? ? int w = num%2;
? ? ? ? ? ?System.out.print(w+" "); ?
? ? ? ?} ?
? ? ? ?else{ ?
? ? ? ? int w = num%2;
? ? ? ? ? ?toBinary(num/2);
? ? ? ? ? ?System.out.print(w+" "); ?
? ? ? ?}?
? ?} ?
? ? ?}
2015-03-22
額,這是把二進制轉換成十進制的....
2015-03-22
package?com.rui.test; public?class?test?{ ????public?static?void?main(String[]?args)?{ ????????//定義一個二進制的數 ????????String?str="1010"; ???????? ????????//將這個二進制的字符串進行反串 ????????char?[]?ch=str.toCharArray(); ????????for?(int?i?=?0;?i?<?ch.length;?i++)?{ ????????????char?c=ch[i]; ????????????ch[i]=ch[ch.length-i-?1]; ????????????ch[ch.length-i-1]=c; ????????????if?((ch.length-1)/2?==i)?{ ????????????????break; ????????????} ????????}???? ???????? ????????StringBuffer?strbuff=new?StringBuffer(String.copyValueOf(ch)); ????????int?num=0; ????????for?(int?i?=?0;?i?<?strbuff.length();?i++)?{ ????????????num+=(strbuff.charAt(i)-48)*Math.pow(2,i); ????????} ????????System.out.println(num); ????} }