導航:首頁 > 編程語言 > java十進制轉2816進制

java十進制轉2816進制

發布時間:2024-04-19 00:46:12

java實現:將一個十進制數分別轉換成二進制,八進制和十六進制,用戶界面如圖 (必須Java實現)

Java源程序

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

{

JTextFieldtxtDec;

JTextFieldtxtBin;

JTextFieldtxtOct;

JTextFieldtxtHex;

publicTest25(){

("十進制整數轉換");

txtDec=newJTextField(15);

txtBin=newJTextField(15);

txtOct=newJTextField(15);

txtHex=newJTextField(15);

txtBin.setEditable(false);

txtOct.setEditable(false);

txtHex.setEditable(false);

this.setLayout(newFlowLayout());

this.add(newJLabel("十進制"));

this.add(txtDec);

this.add(newJLabel("二進制"));

this.add(txtBin);

this.add(newJLabel("八進制"));

this.add(txtOct);

this.add(newJLabel("十六進制"));

this.add(txtHex);

txtDec.addActionListener(this);

this.setSize(250,150);

this.setResizable(false);

this.setVisible(true);

}

publicstaticvoidmain(String[]args){

newTest25();

}

@Override

publicvoidactionPerformed(ActionEvente){

JTextFieldtxt=(JTextField)e.getSource();

if(txt.getText()==null||txt.getText().trim().equals("")){

JOptionPane.showMessageDialog(this,"十進制數不能為空");

return;

}

intnum;

try{

num=Integer.parseInt(txt.getText());

if(num<0){

num*=-1;

txtBin.setText("-"+Convert.format(num,2));

txtOct.setText("-"+Convert.format(num,8));

txtHex.setText("-"+Convert.format(num,16));

}

else{

txtBin.setText(Convert.format(num,2));

txtOct.setText(Convert.format(num,8));

txtHex.setText(Convert.format(num,16));

}

}

catch(Exceptionex){

JOptionPane.showMessageDialog(this,"十進制數錯誤");

return;

}

}

}

classConvert{

privatestaticString[]bins={"0","1"};

privatestaticString[]octs={"0","1","2","3","4","5","6","7"};

privatestaticString[]hexs={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};

publicstaticStringformat(intnum,intr){

StringBufferbuff=newStringBuffer();

if(r==2){

while(num>0){

buff.append(bins[num%r]);

num/=r;

}

}

elseif(r==8){

while(num>0){

buff.append(octs[num%r]);

num/=r;

}

}

elseif(r==16){

while(num>0){

buff.append(hexs[num%r]);

num/=r;

}

}

buff.reverse();

returnbuff.toString();

}

}

運行測試:

② java實現:整數進制相互轉換

java實現:整數進制相互轉換的方法:

  1. 十進制轉為二進制、八進制、十六進制, 用 Integer.toXXXString()方法

    (1)十進制轉為二進制: Integer.toBinaryString(int i);

    public static String toBinaryString(inti):以二進制(基數 2)無符號整數形式返回一個整數參數的字元串表示形式。

    (2)十進制轉為8進制 :Integer.toOctalString(int i);
    public static String toOctalString(inti):以八進制(基數 8)無符號整數形式返回一個整數參數的字元串表示形式。

    (3)十進制轉為16進制: Integer.toHexString(int i);

    public static String toHexString(inti):以十六進制(基數 16)無符號整數形式返回一個整數參數的字元串表示形式。

舉例:

Stringbin=Integer.toBinaryString(10);
Stringoct=Integer.toOctalString(10);
Stringhex=Integer.toHexString(10);

2.十六進制、八進制、二進制轉為十進制(有兩種方法):parseInt() 和valueOf()。

Integer.parseInt(String s,int radix);

public static int parseInt(String s,int radix) throws NumberFormatException{}使用第二個參數作為指定的基數,將字元串參數解析為有符號的整數;

Integer.valueOf(String s,int radix);

public static Integer valueOf(Strings, intradix) throws NumberFormatException{}返回一個 Integer 對象,該對象中保存了用第二個參數提供的基數進行解析時從指定的 String 中提取的值。

舉例:

try{
inti=Integer.parseInt("a",16);
//輸出為10進制數10
System.out.println(i);
}catch(Exceptione){
e.printStackTrace();
}
try{
//十六進制轉成十進制
Stringhex=Integer.valueOf("FFFF",16).toString();
System.out.println(hex);
//八進制轉成十進制
Stringoct=Integer.valueOf("12",8).toString();
System.out.println(oct);
//二進制轉十進制
Stringbin=Integer.valueOf("0101",2).toString();
System.out.println(bin);
}catch(Exceptione){
e.printStackTrace();
}

③ 用JAVA將十進制轉換成十六進制

1、用來Integer.toHexString方法即可將十進制裝自成十六進制。

package com.test;

public class Test {

public static void main(String[] args) {
int i = 123;
System.out.println(Integer.toHexString(i));
}
}

④ java中怎麼將十進制轉換為十六進制

用除商取余法
用十進制數除以16 記錄下商和余數
然後繼續用商去除以16記錄下商和余數,直到商為0
最後將余數逆序排列就可以了
比如十進制數33轉為十六進制
33/16=2 餘1
2/16=0 餘2
結果就是0x 21(十六進制以0x開頭)

⑤ java中怎麼將10進制轉化成十六進制

Scanner sc = new Scanner(System.in);
System.out.println("請出入一個十進制數");
int x = sc.nextInt();
System.out.println(x+"的十六進制結果是:"+Integer.toHexString(x));

⑥ 請問怎麼用java編程語言,將10進制的數字轉為2進制,8進制,16進制具體代碼怎麼寫

import java.io.*;
class test
{
static BufferedReader keyboard=new
BufferedReader(new InputStreamReader(System.in));
public static void main(String[]args)throws IOException
{
int n,base;
n=Integer.parseInt(keyboard.readLine());
exchange(n);
System.out.println();
}
public static void exchange(int n)
{
if(n>0)
{
exchange(n/2);
System.out.print(n%2);
}
}
}

這個是十進制轉化為二進制的
用的是遞歸方法

八進制與十六進制思路相同

閱讀全文

與java十進制轉2816進制相關的資料

熱點內容
查國家文件去哪裡查 瀏覽:722
手機上怎麼發文件 瀏覽:64
數據分析過程6個步驟是哪些 瀏覽:622
baksmali最新版本 瀏覽:666
小米查找手機在哪個文件夾 瀏覽:906
編程進銷存用什麼語言 瀏覽:412
linux下獲取目錄下所有文件名 瀏覽:422
note5怎麼退出應用程序 瀏覽:71
qq個性群網名 瀏覽:224
激盪三十年版本哪個好 瀏覽:950
移動內網怎麼傳文件夾 瀏覽:581
extjslicense 瀏覽:338
文件夾變成ink 瀏覽:124
七彩虹h110裝機教程 瀏覽:351
word三個減號回車 瀏覽:844
生存之旅22041升級補丁 瀏覽:825
強行進入別人微信空間 瀏覽:208
win81有線未識別網路 瀏覽:616
a7m3圖片配置文件視頻 瀏覽:471
linux設備驅動程序開發步驟 瀏覽:640

友情鏈接