十进制化二进制、八进制、十六进制程序
//十进制化二进制、八进制、十六进制程序
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class ChenWin502 extends JApplet{
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3,t4;
JTextArea jt;
JScrollPane jsp;
Container con;
JPanel p;
public void init(){
con=getContentPane();
l1=new JLabel("输入一个十进制正整数:",JLabel.RIGHT);
l2=new JLabel("二进制数是:",JLabel.RIGHT);
l3=new JLabel("八进制数是:",JLabel.RIGHT);
l4=new JLabel("十六进制数是:",JLabel.RIGHT);
t1=new JTextField("1",10);
t1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int a=Integer.parseInt(t1.getText());
t2.setText(gettwo(a));
t3.setText(geteight(a));
t4.setText(getsixten(a));
}
});
t2=new JTextField("1",36);
t3=new JTextField("1",14);
t4=new JTextField("1",14);
jt=new JTextArea();
jsp=new JScrollPane(jt);
p=new JPanel();
p.setLayout(new GridLayout(4,2));
Border bo1=BorderFactory.createRaisedBevelBorder();
Border bo2=BorderFactory.createTitledBorder("Ctrl");
p.setBorder(new CompoundBorder(bo1,bo2));
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(l3);
p.add(t3);
p.add(l4);
p.add(t4);
con.add(p,BorderLayout.NORTH);
con.add(jsp,BorderLayout.CENTER);
jt.append("十进制 二进制 八进制 十六进制\n");
for(int i=1;i<100;i++){
jt.append(Integer.toString(i)+" ");
jt.append(gettwo(i)+" ");
jt.append(geteight(i)+" ");
jt.append(getsixten(i)+" \n");
}
}
public String gettwo(int value){
int displayMask=1<<31;
StringBuffer buf=new StringBuffer(35);
for(int c=1;c<=32;c++){
buf.append((value&displayMask)==0?0:1);
value<<=1;
}
return buf.toString();
}
public String geteight(int value){
int j=0;
StringBuffer buff=new StringBuffer(12);
String twos="0"+gettwo(value);
for(int i=1;i<=11;i++){
String sub=new String(twos.substring(j,j+3));
if(sub.equals("000")) buff.append(0);
else if(sub.equals("001")) buff.append(1);
else if(sub.equals("010")) buff.append(2);
else if(sub.equals("011")) buff.append(3);
else if(sub.equals("100")) buff.append(4);
else if(sub.equals("101")) buff.append(5);
else if(sub.equals("110")) buff.append(6);
else buff.append(7);
j+=3;
}
String eight=buff.toString();
int k=0;
if(value==0) return "0";
else{
while(eight.charAt(k)==0){
k++;}
return "0"+eight.substring(k);
}
}
public String getsixten(int value){
int j=0;
StringBuffer buff=new StringBuffer(12);
String twos=gettwo(value);
for(int i=1;i<=8;i++){
String sub=new String(twos.substring(j,j+4));
if(sub.equals("0000")) buff.append(0);
else if(sub.equals("0001")) buff.append(1);
else if(sub.equals("0010")) buff.append(2);
else if(sub.equals("0011")) buff.append(3);
else if(sub.equals("0100")) buff.append(4);
else if(sub.equals("0101")) buff.append(5);
else if(sub.equals("0110")) buff.append(6);
else if(sub.equals("0111")) buff.append(7);
else if(sub.equals("1000")) buff.append(8);
else if(sub.equals("1001")) buff.append(9);
else if(sub.equals("1010")) buff.append(a);
else if(sub.equals("1011")) buff.append(b);
else if(sub.equals("1100")) buff.append(c);
else if(sub.equals("1101")) buff.append(d);
else if(sub.equals("1110")) buff.append(e);
else buff.append(f);
j+=4;
}
String sixten=buff.toString();
int k=0;
if(value==0) return "0";
else {
while(sixten.charAt(k)==0){
k++;}
return "0x"+sixten.substring(k);
}
}
}