package routingtile;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.omg.CORBA.PRIVATE_MEMBER;
public class HexFinder {
public static String format(byte[] bt) {
int line = 0;
StringBuilder buf = new StringBuilder();
for (byte d : bt) {
if (line % 16 == 0)
buf.append(String.format("%05x: ", line));
buf.append(String.format("%02x ", d));
line++;
if (line % 16 == 0)
buf.append("\n");
}
buf.append("\n");
return buf.toString();
}
public static byte[] readFile(String file) throws IOException {
InputStream is = new FileInputStream(file);
int length = is.available();
byte bt[] = new byte[length];
is.read(bt);
return bt;
}
/**
* 十六进制转二进制
*
* @param hexString
* @return
*/
public static String hexString2binaryString(String hexString) {
if (hexString == null || hexString.length() % 2 != 0)
return null;
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++) {
tmp = "0000"
+ Integer.toBinaryString(Integer.parseInt(
hexString.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);
}
return bString;
}
/**
* 读取十六进制文件后转成二进制
* @return strB
* @throws IOException
*/
public static StringBuilder readData() throws IOException{
//读取十六进制文件
byte[] bt = HexFinder.readFile("./first.dat");
String hexData = HexFinder.format(bt);
//按空格切割
String[] re = hexData.split("\n");
String sixData;
StringBuilder strB = new StringBuilder();
for (String string : re) {
String[] result = string.split("\\s+");
for (String s : result) {
if(s.length()>5){
continue;
}else{
//十六转二
sixData = hexString2binaryString(s);
strB.append(sixData);
}
}
}
return strB;
}
protected static final int BITS_BYTE = 8;
protected static final int simpleIntersectionOffset = 90;
protected static final int numIntersections = 16;
protected static final int BITS_OFFSET = 14;
private static Boolean flag = true;
/**
* 将type数组按“:”切割,并截取strB中的位数
* @param type
* @param strB
* @return
* @throws IOException
*/
public static HashMap<String, Integer> dicedsimpleIntersection(int offset,int pointer,StringBuilder strB) throws IOException{
HashMap<String, Integer> mp = new HashMap<String, Integer>();
String a;
String lastValue;
int index;
//从736开始读取交叉点二进制中的数据
final int numInter = BITS_BYTE * simpleIntersectionOffset + numIntersections;
//第一次从736开始读
if(flag){
//模拟指针
index = numInter + offset;
a = strB.substring(numInter,index).trim();
flag = false;
lastValue = Long.valueOf(a,2).toString();
//放入mp中
mp.put("pointer", index);
mp.put("value", Integer.parseInt(lastValue));
}else{
//模拟指针
index = pointer + offset;
a = strB.substring(pointer,index).trim();
lastValue = Long.valueOf(a,2).toString();
//放入mp中
mp.put("pointer", index);
mp.put("value", Integer.parseInt(lastValue));
//如果是十四位并且首位是一进行一下操作
String firstPosition = a.substring(0, 1);
if(offset == BITS_OFFSET && firstPosition.equals("1")){
a = strB.substring(pointer+1,index).trim();
lastValue = Long.valueOf(a,2).toString();
//放入mp中
mp.put("pointer", index);
mp.put("value", -Integer.parseInt(lastValue));
}
}
return mp;
}
/*
*//**
* 将type数组按“:”切割,并截取strB中的位数
* @param type
* @param strB
* @throws IOException
*//*
public static void dicedWrite(String[] type,StringBuilder strB) throws IOException{
String s;
String beginName;
int a;
int b = 0;
for(String ty:type){
//切割字符串
int begin = ty.indexOf(":");
int last = ty.length();
beginName = ty.substring(0,begin);
a = Integer.parseInt(ty.substring(begin+1,last).trim());
if(b==0){
s = strB.substring(a-1, a);
}else{
s = strB.substring(b, a+b);
}
//如果前十位都是boolean不能被8整除当占32位时就跳过六位
if(b==10 && a==32){
s = strB.substring(b+2*8-10, a+b+2*8-10);
b = b+2*8-10;
}
b = b+a;
System.out.println("-");
String lastValue = Long.valueOf(s,2).toString();
//十进制转换
System.out.println(beginName+":"+lastValue);
write(beginName,lastValue);
//将所有得值以键值对的形式存放到Map中,值是前面的占位方便之后使用
resultMap.put(beginName,lastValue);
}
}*/
/*
*//**
* 将type数组按“:”切割,并截取strB中的位数
* @param type
* @param strB
* @throws IOException
*//*
public static void dicedWrite(String[] type,StringBuilder strB,String index) throws IOException{
String s;
String beginName;
int a;
int b = 0;
for(String ty:type){
//切割字符串
int begin = ty.indexOf(":");
int last = ty.length();
beginName = ty.substring(0,begin);
a = Integer.parseInt(ty.substring(begin+1,last).trim());
if(b == 0){
b = Integer.parseInt(index)*8;
s = strB.substring(b, a+b);
}else{
s = strB.substring(b, a+b);
}
b = b+a;
System.out.println("-");
String lastValue = Long.valueOf(s,2).toString();
//十进制转换
System.out.println(beginName+":"+lastValue);
write(beginName,lastValue);
//将所有得值以键值对的形式存放到Map中,值是前面的占位方便之后使用
resultMap.put(beginName,String.valueOf(b));
}
}*/
public static void write(String name,String hexString) throws IOException {
/**
* 创建一个可以往文件中写入字符数据的字符流输出流对象
* 创建时必须明确文件的目的地
* 如果文件不存在,这回自动创建。如果文件存在,则会覆盖。
* 当路径错误时会抛异常
*
* 当在创建时加入true参数,回实现对文件的续写。
*/
FileWriter fw = new FileWriter(".\\demo1.txt",true);
/**
* 调用该对象的write方法,向文件写入字符。
*
* 其实写入到了临时存储缓冲区中
*/
fw.write(name+":"+hexString+"\r\n");//windows中的换行为\r\n
/**
* 进行刷新,将字符写到目的地中。
*/
fw.flush();
/**
* 关闭流,关闭资源。在关闭前会调用flush方法 刷新缓冲区。关闭后在写的话,会抛IOException
*/
fw.close();
}
}