展开全部
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class BitChanger {
public static void main(String[] args) {
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
String s="HELLO";
System.out.println(s);
System.out.println(toBinaryString(s));
System.out.println(toHexString(s));
System.out.println(changeBit(toBinaryString(s),12));
System.out.println(toNormalString(changeBit(toBinaryString(s),12)));
System.out.println(toHexString(toNormalString(changeBit(toBinaryString(s),12))));
JFrame f=new JFrame("Bit changer v1.0");
f.setSize(433,230);
f.setDefaultCloseOperation(3);
f.getContentPane().setLayout(null);
JLabel l1=new JLabel("Enter a string:");
final JTextField input=new JTextField(20);
final JLabel binary=new JLabel();
final JLabel hex=new JLabel();
JLabel l2=new JLabel("Enter the Bit number to flip:");
final JTextField flip=new JTextField(5);
JButton change=new JButton("Change
Bit");
JButton exit=new JButton("Exit");
l1.setBounds(10,10,100,25);
input.setBounds(110,10,100,25);
binary.setBounds(10,45,400,25);
hex.setBounds(10,80,400,25);
l2.setBounds(10,115,208,25);
flip.setBounds(220,115,40,25);
change.setBounds(270,115,68,45);
exit.setBounds(123,150,60,30);
f.add(l1);
f.add(l2);
f.add(input);
f.add(binary);
f.add(hex);
f.add(flip);
f.add(change);
f.add(exit);
f.setVisible(true);
binary.setBorder(BorderFactory.createLineBorder(Color.blue));
hex.setBorder(BorderFactory.createLineBorder(Color.red));
input.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e) {
binary.setText(toBinaryString(input.getText()));
hex.setText(toHexString(input.getText()));
}});
change.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
int id=Integer.parseInt(flip.getText().trim());
input.setText(toNormalString(changeBit(toBinaryString(input.getText()),id)));
}catch(Exception ex){}
}});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}});
}
private static String toNormalString(String binary){
String[] sp=binary.split(" ");
String v="";
for(int i=0; i
v+=(char)Integer.valueOf(sp[i],2).intValue();
}
return v;
}
private static String changeBit(String s,int bi){
if(s.isEmpty())return s;
int cid=bi/8;
int bid=bi%8;
char ch=s.charAt(cid*9+bid-1);
ch=ch=='0'?'1':'0';
s=s.substring(0,cid*9+bid-1)+ch+s.substring(cid*9+bid);
return s;
}
private static String toBinaryString(String s) {
if(s.isEmpty())return s;
String v="";
for(int i=0; i
String bs=Integer.toBinaryString(s.charAt(i));
while(bs.length()<8)
bs='0'+bs;
v+=bs+" ";
}
return v;
}
private static String toHexString(String s){
if(s.isEmpty())return s;
String v="";
for(int i=0; i
String bs=Integer.toHexString(s.charAt(i));
v+=" "+bs.toUpperCase()+" ";
}
return v;
}
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起