① 用java語言將二進制轉化成十進制的代碼

import java.util.Scanner; public class S_T { public static void main(String[] args) { int x=0,y=0; Scanner scanner = new Scanner(System.in); try { x = scanner.nextInt(); }catch(java.util.InputMismatchException ime) { //if the next token does not match the Integer regular //expression, or is out of range System.out.println("not a integer"); } System.out.println("輸入的二進制數是:"+x); int n=0,t=1; while(x>0) { y+=x%10*t; x=x/10; n++; t=t*2; } System.out.print("輸出的十進制數是:"+y); } } 要轉化的二進制數在控制台中輸入..

② java中有將二進制轉換成十進制的方法嗎

:java進制轉換 java中進行二進制,八進制,十六進制,十進制間進行相互轉換 十進制轉成十六進制: Integer.toHexString(int i) 十進制轉成八進制 Integer.toOctalString(int i) 十進制轉成二進制 Integer.toBinaryString(int i) 十六進制轉成十進

③ java中如何將十進制數字轉化成二進制

如果表達小於2^31-1的正數代碼如下:

public void binaryToDecimal(int n){

int t = 0; //用來記錄位數

int bin = 0; //用來記錄最後的二進制數

int r = 0; //用來存儲余數

while(n != 0){

r = n % 2;

n = n / 2;

bin += r * Math().pow(10,t);

t++;

}

System.out.println(bin);

}

使用字元串的拼接(+)來實現大於2^31-1的數,代碼如下:

public void binaryToDecimal(int n){

String str = "";

while(n!=0){

str = n%2+str;

n = n/2;

}

System.out.println(str);

}

可以右移後再與0x01進行&運算得到第一位的數字判斷判斷它的第幾位上是0,第幾位上是1,代碼如下:

class ByteMove

{

public static void main(String[] args)

{

int i = 7;

move(i);

}

static void move(int num){

for(int i= 0;i<6;i++){

System.out.println("第"+(i+1)+"位:" + (num >> i & 0x01));

}

}

}

④ java 輸入一個二進制數,把它轉換為十進制數並輸出

importjava.util.Scanner;

publicclassTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
System.out.println("輸入一個二進制數:");
Stringa=sc.nextLine();
intd=Integer.parseInt(a,2);//2進制
into=Integer.parseInt(a,8);//8進制
System.out.println("二進制轉版為十進制:"+d);
System.out.println("八進權制轉為十進制:"+o);
}
}

⑤ java二進制字元串轉十進制

publicintconvertToDecimal(Stringbinary){
returnInteger.valueOf(binary,2);
}

⑥ 用JAVA把二進制數轉換成十進制數。

一樓回答的非常詳盡了哈,

就是感覺太多了,

個人覺得,比較常用的就是Integer的parseInt()方法,

使用起來也非常方便:

int a = Integer.parInt("你的二進制數", 2);

⑦ JAVA編程二進制怎樣轉換成十進制

import java.math.BigDecimal;

public class Test {

public static void main(String[] args) {
Test t = new Test();
double d = 8;
String s = t.decimal2BinaryStr(d);
System.out.println("十進制數"+d+"轉成二進制數為:"+s);
}

/**
* 十進制數轉二進制數
* @param d 十進制數
* @return 十進制數轉換成二進制的字元串
*/
public String decimal2BinaryStr(double d){
String result = decimal2BinaryStr_Inte(d);
result += decimal2BinaryStr_Deci(d);
return result;
}

/**
* 十進制整數部分轉二進制數
* @param d 十進制數
* @return 十進制整數部分轉換成二進制的字元串
*/
public String decimal2BinaryStr_Inte(double d){
// return Integer.toBinaryString((int)d);
/*
* 本來利用上面的Integer.toBinaryString(int)就可以得到整數部分的二進制結果,
* 但為了展示十進制轉二進制的演算法,現選擇以下程序來進行轉換
*/
String result = "";
long inte = (long)d;
int index = 0;
while(true){
result += inte%2;
inte = inte/2;
index++;
if(index%4 == 0){
result+=" ";
}
if(inte==0){
while(index%4!=0){
result+="0";
index++;
}
break;
}
}
char[] c = result.toCharArray();
char[] cc = new char[c.length];
for(int i=c.length; i>0; i--){
cc[cc.length-i] = c[i-1];
}
return new String(cc);
}

/**
* 十進制小數部分轉二進制
* @param d 十進制數
* @return 十進制小數部分轉換成二進制小數的字元串
*/
public String decimal2BinaryStr_Deci(double d){
return decimal2BinaryStr_Deci(d, 0);
}

/**
* 十進制小數部分轉二進制
* @param d 十進制數
* @param scale 小數部分精確的位數
* @return 十進制小數部分轉換成二進制小數的字元串
*/
public String decimal2BinaryStr_Deci(double d, int scale){
double deci = sub(d,(long)d);
if(deci==0){
return "";
}
//為了防止程序因所轉換的數據轉換後的結果是一個無限循環的二進制小數,因此給其一個默認的精確度
if(scale==0){
scale = (String.valueOf(deci).length()-2)*4;
}
int index = 0;
StringBuilder inteStr = new StringBuilder();
double tempD = 0.d;
while(true){
if(deci==0 || index==scale){
while(index%4!=0){
inteStr.append("0");
index++;
}
break;
}
if(index==0){
inteStr.append(".");
}
tempD = deci*2;
inteStr.append((int)tempD);
deci = sub(tempD ,(int)tempD);
index++;
if(index%4 == 0){
inteStr.append(" ");
}
}
return inteStr.toString();
}

/**
* 提供精確的減法運算。
* @param v1 被減數
* @param v2 減數
* @return 兩個參數的差
*/
public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
}

⑧ java二進制轉十進制

(4)2^7-1-2^(5-1)
(6)2^32-1-2^3-2^1-1
(7)2^32-1-2^7-2^1
思路:
當從右往左數為第n位時,該位置的表示2^(n-1);
比如1011左數第一位的1表示8=2^(4-1)
當從右往左數的前n位全為1時,該數表示2^n-1;
比如1111=15=2^4-1
所以當首位是1,且1明顯比0多時,採用減法會簡單很多
第七題就是全為1時減去右數第八位和第二位的1後所得的結果
希望我的回答對你有用。

⑨ JAVA中的十進制轉換為二進制

public class test {
public static void main(String[] args) {
String m = Integer.toBinaryString(120);
System.out.println(m);
}
}
--------------------------------

public class test {
public static void main(String[] args) {
int m = 13;
String s = "";
while (m > 0) {
s = m % 2 + s;
m = m / 2;
}
System.out.println(s);
}
}