⑴ java中如何把十六進制轉為十進制
分類: 電腦/網路 >> 程序設計 >> 其他編程伍亮語言
問題描述:
我用的java1.4 請問如何將十六進制整形數轉化為十進制數橘沖的
解析:
import java.awt.*;
public class d2x extends Frame
{
int decimalValue= 0;
String baseXValue = new String("0");
TextField dDisplay,xDisplay;
d2x constructor
d2x()
{
super("Decimal Converter");set the title of the frame
MenuBar mb = new MenuBar();
Button d2Binary = new Button("Binary");
Button d2Octal = new Button("Octal");
Button d2Hex = new Button("Hex");
Button d2Base36 = new Button("Base36");
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
add a simple menu
Menu m = new Menu("Application");
m.add(new CheckboxMenuItem("Base 36 Active"));
m.add(new MenuItem("Exit"));
add menu to menubar
mb.add(m);
setMenuBar(mb);install this menu bar in the frame
Add buttons to their own panel
p3.setLayout(new FlowLayout());
p3.add(d2Binary);
p3.add(d2Octal);
p3.add(d2Hex);
p3.add(d2Base36);
Add text fields
Label dLabel = new Label("Enter Deecimal: ");
Label xLabel = new Label("Converted Value: ");
dDisplay = new TextField(Integer.toString(decimalValue),7);
xDisplay = new TextField(baseXValue,32);
xDisplay.setEditable(false);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(dLabel);
p1.add(dDisplay);
p2.add(xLabel);
p2.add(xDisplay);
Add the panels
add("North",p1);
add("Center",p2);
add("South"薯殲,p3);
}end d2x constructor
public void start()
{
resize(400,150);
show();
}
public void updateXDisplay()
{
xDisplay.setText(baseXValue);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof MenuItem)
{
if ("Exit".equals(((MenuItem)evt.target).getLabel()))
{
hide();
dispose();
System.exit(0);
return false;
}
return true;
}
else if(evt.target instanceof Button)
{
String whick = ((Button)evt.target).getLabel();
if (whick.equals("Binary"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,2);
}
if (whick.equals("Octal"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,8);
}
if (whick.equals("Hex"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,16);
}
if (whick.equals("36"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,36);
}
updateXDisplay();
return true;
}
return false;
}
public static void main(String args[])
{
d2x m = new d2x();
m.start();
}
}
⑵ JAVA 進制加減乘除的問題~ 急等!!
2、8、16進制的運算,目前sun還沒有推出相關的內容。不過java提供了10進制轉換成2、8、16進制的演算法已經2、8、16進制轉換成10進制的演算法。
十進制轉成十六進制:
Integer.toHexString(int i)
十進制轉成八進制
Integer.toOctalString(int i)
十進制轉成二進制
Integer.toBinaryString(int i)
十六進制轉成十進制
Integer.valueOf("FFFF",16).toString()
八進制轉成十進制
Integer.valueOf("876",8).toString()
二進制轉十進制
Integer.valueOf("0101",2).toString()
你可以自己寫一個2進制的演算法。
有種方法可以比較簡單實現。
1、例如x、y是輸入的二進制的數
public static void main(String[] args){
String x = "101001";
String y = "100101";
int x1 = Integer.valueOf(x,2).toString();
int y1 = Integer.valueOf(y,2).toString();
int z = x1 + y1;
String result = Integer.toBinaryString(z);
System.out.println(result);
}
變數result就是二進制的結果了。
8、16進制的運行與上面的方法基本一致,稍微修改一下轉換函數就可以了。
順便提一句,8、16進制與2進制轉換是簡單了。8進制的一個數字對應2進制的3個數字,16進制的一個數字對應2進制的4個數字。
例如:8進制:數字457,將數字分開成4#5#7,對應的2進制 100#101#111
⑶ 關於java中16進制的問題
因為int 為帶符號類型,帶符號類型最高為是符號位,又因為0xFFFFFFFF,也就是四個位元組32 bits全是1, 符號位是1,所以這個數是負數。
內存中的數值為補碼表示,所以0xFFFFFFFF是一個負數的補碼。負數從補碼求原碼,最高符號位不變,保持 1, 其餘各位求反,末尾加1,也就是 0xFFFFFFFF,二進制為:11111111 11111111 11111111 11111111
-> 10000000 00000000 00000000 00000000
-> 10000000 00000000 00000000 00000001
原碼首位表示符號位,其餘位表示絕對值大小,所以,這個數是 -1
而0xFF轉換為二進制為:00000000 0000000 00000000 11111111其高位為0,即為正數,正數的原碼,反碼,補碼相同,所以值為255
⑷ JAVA進(JAVA程序關於八進制和16進制)
只向你講解一下整數部分的轉換規則。至於小數部分,則比較復雜。
先說非十進制轉十進制,比較簡單:
將數從右到左編號,最右邊的編號是0,右邊第二位編號為1,依此類推.
將給定數的各位值乘以進制的編號次方,再得到的結果相加即可.說起來不好理解,舉個例子:
二進制:10101
編號: 43210
計算:1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0
= 1 * 16 + 0*8 + 1*4 + 1*1 = 21(十進制)
十六進制:0xA9D3
編號:3210
計算:A*16^3 + 9*16^2 + D*16^1 + 3*16^0
=10*4096 + 9*256 + 13*16 + 3 = 43475(十進制)
不信可以用WINDOWS自帶的計算器驗證.(查看->科學型).
====
至於十進制轉非十進制,比較麻煩,要用到 連續除法求余 的計算.
用給定的數除以進制(如8,16,2),將得到商和余數,再將商除以進制,又得到商和余數.一直除到商為0為止.
然後將 所有得到的余數 按照出現的 相反 的順序排列起來,即得到結果.
舉例:
124(十進制)轉二進制,連續除以"2",運算如下:
被除數/進制 = 商……余數
124 /2 = 62……0
62 /2 = 31……0
31 /2 = 15……1
15 /2 = 7……1
7 /2 = 3……1
3 /2 = 1……1
1 / 2 = 0……1
將所有出現的余數從後向前排列,就得到最後結果為:1111100
同樣,將其轉為八進制:
124 /8 = 15……4
15 /8 = 1……7
1 /8 = 0……1
結果即為174(八進制).
同樣可在計算器上試驗一下.
⑸ java中求16進制異或和
publicclassTest{
publicStringa;
publicstaticvoidmain(String[]args){
int[]a=newint[]{(int)0X8A,(int)0X12,(int)0X05,
(int)0X07,(int)0XFE,(int)0XE3,(int)0X0A,
(int)0X06,(int)0X10,(int)0X14,(int)0X29};
intChkSum=0;
for(inti=0;i<2;i++){//a.length,只有在為2時結果為98,也就是說只驗證了0X8A,0X12這2個數
ChkSum=(int)(ChkSum^a[i]);
}
System.out.println(Integer.toHexString(ChkSum)+"H");
}
}
N的輸入我就不寫了,自己給了一個固定值2,你把 i<2中的2替換成 輸入值就好