Java对文件的16进制读取和操作
大家可以参考一下源代码的相关部分注释,然后写出自己的16进制处理程序。
有几个重点地方:
- 16进制字符串-》10进制数
int input = Integer.parseInt("Str", 16)
- 10进制整数-》16进制字符串
String hex = Integer.toHexString(int)
- 文件读取方法
作为2进制文件直接读取,一个byte为单位的读取。
将来我还将在此基础上制作Java版本的16进制编辑器,请大家多多支持。谢谢。
/**
* RO Utility
* Mainly used for:
* 1.Double Open client
* 2.Open Unlimited View
* 这是个样本程序,是我针对游戏修改写的。主要作用是将游戏文件用16进制打开,然后
* 修改相关的部分,然后保存。
*
* @author Ciro Deng(cdtdx@sohu.com)
* @version 1.0
*/
package cn.edu.uestc.rotool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
/**
* RO Utility Mainly used for: 1.Double Open client 2.Open Unlimited View
*
* @author Ciro Deng(cdtdx@sohu.com)
* @version 1.0
*
*/
public class MainTool {
private final String RO_HOME = "D://Games//RO//"; //修改文件的路径
private final String FILE = "Ragexe"; //修改文件的主文件名
private final String BAK_FILE = FILE + "_BAK.sp2"; //修改文件的备份扩展名
private final String PATCH_FILE = FILE + ".sp2"; //修改文件的扩展名
/**
* 进行16进制替换的规则定义
* Pattern Array Example: pattern[0][0] = "Original Hex String"; 原16进制字符串
* pattern[0][1] = "New Hex String"; 要替换的16进制字符串
*/
private final String[][] pattern = {
{ "85C074095F5E33C05B8BE55DC3", "85C0EB095F5E33C05B8BE55DC3" },
{ "85C0740E5F5EB801000000", "85C0EB0E5F5EB801000000" }, // Double
// Open
{ "000066430000C843", "0000004300008644" } // Umlimited View
};
/**
* 备份文件恢复
* ture the backup file into real file
*
*/
public void restore() {
if (isExistBackup()) {
new File(RO_HOME + PATCH_FILE).delete();
new File(RO_HOME + BAK_FILE)
.renameTo(new File(RO_HOME + PATCH_FILE));
System.out.println("[----------------Restore file OK!--------------------]");
} else {
System.out.println("Backup file does not exist!");
System.exit(0);
}
}
public void init() { //初始化操作
if (new File(RO_HOME + PATCH_FILE).exists()) {
System.out
.println("[-------------Initialize original file OK!-----------]");
} else {
System.out.println("File is not Existed! Please restore it first!");
}
// backup original file
if (!isExistBackup()) {
new File(RO_HOME + PATCH_FILE)
.renameTo(new File(RO_HOME + BAK_FILE));
}
System.out
.println("[---------------Please choose your action------------]");
System.out.println("1:Modify double open and unlimited view mode!");
System.out.println("2:Restore original mode!");
System.out.println("Please input 1 or 2 and Enter:");
}
public void success() { //成功操作提示
System.out.println();
System.out
.println("[-------------Patch file OK! Have fun with RO!-------]");
}
/**
* 进行16进制替换
* replace input Hex String with defined pattern
*
* @param original
* @return
*/
public String replace(String original) {
for (int i = 0; i < pattern.length; i++) {
original = original.replaceAll(pattern[i][0].toLowerCase(),
pattern[i][1].toLowerCase());
}
return original;
}
/**
* 将文件读取为16进制String
* Read original File and transfer it into Hex String
*
* @return
* @throws IOException
*/
public String readOriginal2Hex() throws IOException {
FileInputStream fin = new FileInputStream(new File(RO_HOME + BAK_FILE));
StringWriter sw = new StringWriter();
int len = 1;
byte[] temp = new byte[len];
/*16进制转化模块*/
for (; (fin.read(temp, 0, len)) != -1;) {
if (temp[0] > 0xf && temp[0] <= 0xff) {
sw.write(Integer.toHexString(temp[0]));
} else if (temp[0] >= 0x0 && temp[0] <= 0xf) {//对于只有1位的16进制数前边补“0”
sw.write("0" + Integer.toHexString(temp[0]));
} else { //对于int<0的位转化为16进制的特殊处理,因为Java没有Unsigned int,所以这个int可能为负数
sw.write(Integer.toHexString(temp[0]).substring(6));
}
}
return sw.toString();
}
/**
* 将替换后的16进制字符串写回文件
* write replaced original String to file
*
* @param replaced
* @throws NumberFormatException
* @throws IOException
*/
public void writeNew2Binary(String replaced) throws NumberFormatException,
IOException {
FileOutputStream fout = new FileOutputStream(RO_HOME + PATCH_FILE);
for (int i = 0; i < replaced.length(); i = i + 2) {
fout.write(Integer.parseInt(replaced.substring(i, i + 2), 16));
}
}
/**
* test direct output string to file
*
* @param temp
* @throws IOException
*/
public void writeTest(String temp) throws IOException {
FileOutputStream fout = new FileOutputStream(RO_HOME + "test.txt");
for (int i = 0; i < temp.length(); i++) {
fout.write(temp.charAt(i));
}
}
/**
* check if the backup file exists
*
* @return
*/
public boolean isExistBackup() {
return new File(RO_HOME + BAK_FILE).exists();
}
/**
* 主要操作方法,组织工作流程
* Main process method
*
* @throws IOException
*/
public void patch() throws IOException {
// init
init();
//输入参数:
//1:进行查找替换
//2:将备份文件恢复
String input = new BufferedReader(new InputStreamReader(System.in))
.readLine();
if (input.equals("1")) {
String temp = null;
temp = readOriginal2Hex();
temp = replace(temp);
writeNew2Binary(temp);
success();
} else if (input.equals("2")) {
restore();
} else {
System.out.println("Bad input parameter!");
System.exit(0);
}
}
/**
* Main方法
* main
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
MainTool tool = new MainTool();
tool.patch();
}
}
Java对文件的16进制读取和操作的更多相关文章
- java byte数组与16进制间的相互转换
java byte数组与16进制间的相互转换 CreationTime--2018年6月11日15点34分 Author:Marydon 1.准备工作 import java.util.Array ...
- Java中char转为16进制
Java中char转为16进制 char a = '0'; String hexStr = Integer.toHexString(a); System.out.println(hexStr);
- Java中字符串转为16进制表示
Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...
- php实现文件与16进制相互转换
php实现文件与16进制相互转换 <pre><?php/** * php 文件与16进制相互转换 * Date: 2017-01-14 * Author: fdipzone * Ve ...
- Java中byte与16进制字符串的互相转换
* Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串. * @param s ...
- [转]Java中byte与16进制字符串的互相转换
Java中byte用二进制表示占用8位,而我们知道16进制的每个字符需要用4位二进制位来表示(23 + 22 + 21 + 20 = 15),所以我们就可以把每个byte转换成两个相应的16进制字符, ...
- Java中byte与16进制字符串的互换原理
我们都知道Java中的byte是由8个bit组成的,而16进制即16中状态,它是由4个bit来表示的,因为24=16.所以我们可以把一个byte转换成两个用16进制字符,即把高4位和低4位转换成相应的 ...
- java字节数组和16进制之间的转换
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...
- Java 将数字转为16进制,然后转为字符串类型
public class ArrayTest3 { public static void main(String[] args){ System.out.println(toHex(60)); } / ...
随机推荐
- UVA 1193 区间相关(greedy)
input n d 1<=n<=1000 n行坐标xi,yi output 位于x轴扫描器的扫描距离为d,至少要多少个扫描器才能扫描到所有坐标 如果无法扫描完输出-1,否则输出扫描器个数 ...
- VMI
在虚拟机外部监控虚拟机内部运行状态的方法被称为虚拟机自省(Virtual Machine Introspection,VMI).VMI允许特权域查看非特权域的运行状态,并能获得被监控虚拟机运行状况相关 ...
- set -x /set +x(linux)
Linux 脚本中生成日志 set -x Posted on 2012-07-25 09:44 紫冰龙 阅读(3946) 评论(0) 编辑 收藏 set -x 与 set +x 在liunx脚本中可用 ...
- 项目中的BaseServlet
BaseServlet代码: import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.Ser ...
- Keychain 浅析
什么是Keychain? 根据苹果的介绍,iOS设备中的Keychain是一个安全的存储容器,可以用来为不同应用保存敏感信息比如用户名,密码,网络密码,认证令牌.苹果自己用keychain来保存Wi- ...
- UIImageView 浅析
UIImageView summary UIImageView极其常用,功能比较专一:显示图片 pooperty @property(nonatomic,retain) UIImage *image; ...
- HDU1162-Eddy's picture(最小生成树)
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- php截取中文字符串,英文字符串,中英文字符串长度的方法
今天学习了php函数截取中文字符串,英文字符串,中英文字符串的函数使用方法.对中英文截取方法不理解,此处先做记录. PHP自带的函数如strlen().mb_strlen()都是通过计算字符串所占字节 ...
- ubuntu上的mysql数据库双机备份设置
配置环境: myslq 5.5.3 + ubuntu server 12.04 一.配置MySQL主服务器(192.168.0.1) 1.增加一个账号专门用于同步 1 mysql>grant r ...
- shell输出不换行符合换行符
输出不换行符 例如 echo "Hello\c" echo " World" //Hello World 输出换行符 echo "username\n ...