像UE一样将二进制文件转换为16进制显示
来源:互联网 发布:梦幻西游手游多开软件 编辑:程序博客网 时间:2024/05/22 10:54
package test;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShowBinary extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public ShowBinary(){
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setLayout(null);
int width = 420;
int height = 160;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int x = (d.width - width) / 2;
int y = (d.height - height) / 2;
this.setLocation(x, y);
this.setSize(width, height);
this.setTitle("转换二进制文件");
this.setResizable(false);
JLabel label = new JLabel("二进制文件:");
this.add(label);
label.setBounds(25, 10, 80, 30);
final JTextField text = new JTextField();
this.add(text);
text.setBounds(100, 10, 200, 25);
text.setEditable(false);
final JFileChooser file = new JFileChooser();
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.add(file);
JButton btn = new JButton("选择");
btn.setBounds(310, 10, 60, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file.showOpenDialog(null);
if(state!=1){
File f = file.getSelectedFile();
text.setText(f.getAbsolutePath());
}
}
});
this.add(btn);
JLabel label1 = new JLabel("可看二进制:");
this.add(label1);
label1.setBounds(25, 50, 80, 25);
final JTextField text1 = new JTextField();
text1.setEditable(false);
this.add(text1);
text1.setBounds(100, 50, 200, 25);
final JFileChooser file1 = new JFileChooser();
file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
this.add(file1);
JButton btn1 = new JButton("选择");
btn1.setBounds(310, 50, 60, 25);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file1.showOpenDialog(null);
if(state!=1){
File f = file1.getSelectedFile();
text1.setText(f.getAbsolutePath());
}
}
});
this.add(btn1);
JButton sure = new JButton("生成");
sure.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = text.getText();
if(filepath==null||(filepath=filepath.trim()).length()==0){
return;
}
int last = filepath.length();
if(filepath.contains(".")){
last = filepath.lastIndexOf(".");
}
String name = filepath.substring(filepath.lastIndexOf(File.separator), last);
String defaultPath = filepath.substring(0, filepath.lastIndexOf(File.separator));
String path = text1.getText();
if(path!=null&&path.trim().length()>0){
defaultPath = path.trim();
}
String targetName = defaultPath + name + ".txt";
create(filepath, targetName);
}
});
this.add(sure);
sure.setBounds(100, 90, 60, 30);
JButton close = new JButton("关闭");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.add(close);
close.setBounds(200, 90, 60, 30);
this.setVisible(true);
}
private void create(String source, String target){
FileOutputStream os = null;
try{
os = new FileOutputStream(target);
byte[] bts = Files.readAllBytes(Paths.get(source));
int i = 1;
for(byte b:bts){
int h = 0xf&(b>>>4);
int l = 0xf&b;
String s = (Integer.toHexString(h)+Integer.toHexString(l)).toUpperCase()+" ";
os.write(s.getBytes());
if(i%16==0){
os.write("\n".getBytes());
}
i++;
}
os.flush();
}catch (Exception e) {
// skip
}finally{
if(os!=null){
try{
os.close();
}catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
new ShowBinary();
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShowBinary extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public ShowBinary(){
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setLayout(null);
int width = 420;
int height = 160;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int x = (d.width - width) / 2;
int y = (d.height - height) / 2;
this.setLocation(x, y);
this.setSize(width, height);
this.setTitle("转换二进制文件");
this.setResizable(false);
JLabel label = new JLabel("二进制文件:");
this.add(label);
label.setBounds(25, 10, 80, 30);
final JTextField text = new JTextField();
this.add(text);
text.setBounds(100, 10, 200, 25);
text.setEditable(false);
final JFileChooser file = new JFileChooser();
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.add(file);
JButton btn = new JButton("选择");
btn.setBounds(310, 10, 60, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file.showOpenDialog(null);
if(state!=1){
File f = file.getSelectedFile();
text.setText(f.getAbsolutePath());
}
}
});
this.add(btn);
JLabel label1 = new JLabel("可看二进制:");
this.add(label1);
label1.setBounds(25, 50, 80, 25);
final JTextField text1 = new JTextField();
text1.setEditable(false);
this.add(text1);
text1.setBounds(100, 50, 200, 25);
final JFileChooser file1 = new JFileChooser();
file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
this.add(file1);
JButton btn1 = new JButton("选择");
btn1.setBounds(310, 50, 60, 25);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file1.showOpenDialog(null);
if(state!=1){
File f = file1.getSelectedFile();
text1.setText(f.getAbsolutePath());
}
}
});
this.add(btn1);
JButton sure = new JButton("生成");
sure.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = text.getText();
if(filepath==null||(filepath=filepath.trim()).length()==0){
return;
}
int last = filepath.length();
if(filepath.contains(".")){
last = filepath.lastIndexOf(".");
}
String name = filepath.substring(filepath.lastIndexOf(File.separator), last);
String defaultPath = filepath.substring(0, filepath.lastIndexOf(File.separator));
String path = text1.getText();
if(path!=null&&path.trim().length()>0){
defaultPath = path.trim();
}
String targetName = defaultPath + name + ".txt";
create(filepath, targetName);
}
});
this.add(sure);
sure.setBounds(100, 90, 60, 30);
JButton close = new JButton("关闭");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.add(close);
close.setBounds(200, 90, 60, 30);
this.setVisible(true);
}
private void create(String source, String target){
FileOutputStream os = null;
try{
os = new FileOutputStream(target);
byte[] bts = Files.readAllBytes(Paths.get(source));
int i = 1;
for(byte b:bts){
int h = 0xf&(b>>>4);
int l = 0xf&b;
String s = (Integer.toHexString(h)+Integer.toHexString(l)).toUpperCase()+" ";
os.write(s.getBytes());
if(i%16==0){
os.write("\n".getBytes());
}
i++;
}
os.flush();
}catch (Exception e) {
// skip
}finally{
if(os!=null){
try{
os.close();
}catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
new ShowBinary();
}
}
0 0
- 像UE一样将二进制文件转换为16进制显示
- UE 将DOS格式转换为UNIX格式
- Android将jar包转换为dex二进制文件
- 用java如何实现将16位图像转换为8位图像
- 将16进制转换为十进制
- vi 中16进制显示二进制文件
- 将领域对像的数据转换为DataSet
- 将Image对像转换为png格式byte数组
- 将二叉查找树转换为其镜像
- UE复制粘贴二进制文件
- 将二进制数据转换为字符串显示
- 将图像转换为文字显示
- 将一列字符串显示为转换为多行显示
- strtol----将16进制字符串转换为整数
- .NET : 如何将16进制颜色代码转换为十进制
- 将16进制字符串转换为整数的函数
- 将10进制转换为2-16进制
- 将字符串转换为16进制保存
- Spring 表达式语言(SpEL)
- Java异常体系结构
- 如何配置自己的Activity,让其他应用调用
- c#字符串的格式化输出
- 中国半导体产业同时获三大支撑千载难逢
- 像UE一样将二进制文件转换为16进制显示
- 求区间(a,b)中与n的互质数
- mysql提示错误#42000You have an error in your SQL syntax; check the manual that corresponds
- ios 单击和双击手势
- Qt 中动态库(.so)静态库(.a)的使用
- Fragment的生命周期
- java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 39 path $
- javascript运行机制之执行顺序详解(修正原作者一处错误)
- MySql:charset和collation的设置