導航:首頁 > 編程語言 > 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進制相關的資料

熱點內容
nsh是什麼文件 瀏覽:119
手機QQ怎麼不能傳文件 瀏覽:450
u盤復制文件的速度取決於什麼 瀏覽:659
iphone4下拉通知欄 瀏覽:427
javasocket接收數據包 瀏覽:137
如何給類數據成員變數設置默認值 瀏覽:885
密集app怎麼推廣 瀏覽:866
WP7屏幕保護程序 瀏覽:568
win10雙硬碟老藍屏 瀏覽:986
怎麼設置網站描述 瀏覽:274
微信壓縮包可以發送多大文件 瀏覽:916
u盤打開很卡無法復制裡面文件 瀏覽:143
app怎麼刪不掉 瀏覽:324
農村工作一號文件圖片 瀏覽:598
錘子手機怎麼樣升級 瀏覽:706
微信朋友圈哲理說說 瀏覽:649
jscss模板 瀏覽:472
圖片文件夾里多了一個 瀏覽:305
索尼a7r2配置文件 瀏覽:706
獲取jsp中a中text 瀏覽:396

友情鏈接