62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexParser {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/SUPERSTK.DAD";//Change it to the actual file path
public static void main(String[] args) {
try {
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = toStringHex(bytes[0]) + toStringHex(bytes[1])
+ toStringHex(bytes[2]) + toStringHex(bytes[3]);
if(hexStr.equalsIgnoreCase("FFFFFFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
in.close();
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 4);
getItemX(bytes, 4, 4, 5);
getItemX(bytes, 8, 4, 6);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
getItemX(bytes, 12, 4, 3);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(((long)Integer.parseInt(hexStr, 16))*1000);
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
}
[/quote]
还有点小问题,修改了下, 用下面的替换原来的方法,其它的自己改了:
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(((long)Integer.parseInt(hexStr, 16))*1000);
System.out.print("Date: " + sdf.format(cal.getTime()) + "|" + hexStr.toUpperCase());
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
String hexStr = "";
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
hexStr = "0" + value;
else{
hexStr = Integer.toHexString(value&0xFF);
if(hexStr.length() == 1){
hexStr = "0" + hexStr ;
}
}
return hexStr;
}
[/quote]
能否解释一下toStringHex(byte value)这个方法的逻辑?
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexParser {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/data.dat";//Change it to the actual file path
public static void main(String[] args) {
try {
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = Integer.toHexString(((bytes[0] << 24)&0xFFFF)
| ((bytes[1] << 16)&0xFFFF)
| ((bytes[2] << 8)&0xFFFF)
| (bytes[3]&0xFFFF));
if(hexStr.equalsIgnoreCase("FFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 3);
getItemX(bytes, 4, 4, 4);
getItemX(bytes, 8, 4, 5);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(Integer.parseInt(hexStr, 16));
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
}
[/quote]
还把 in.close(); 加上吧;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexParser {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/data.dat";//Change it to the actual file path
public static void main(String[] args) {
try {
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = Integer.toHexString(((bytes[0] << 24)&0xFFFF)
| ((bytes[1] << 16)&0xFFFF)
| ((bytes[2] << 8)&0xFFFF)
| (bytes[3]&0xFFFF));
if(hexStr.equalsIgnoreCase("FFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 3);
getItemX(bytes, 4, 4, 4);
getItemX(bytes, 8, 4, 5);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(Integer.parseInt(hexStr, 16));
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexParser {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/SUPERSTK.DAD";//Change it to the actual file path
public static void main(String[] args) {
try {
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = toStringHex(bytes[0]) + toStringHex(bytes[1])
+ toStringHex(bytes[2]) + toStringHex(bytes[3]);
if(hexStr.equalsIgnoreCase("FFFFFFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
in.close();
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 4);
getItemX(bytes, 4, 4, 5);
getItemX(bytes, 8, 4, 6);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
getItemX(bytes, 12, 4, 3);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(((long)Integer.parseInt(hexStr, 16))*1000);
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
}
[/quote]
还有点小问题,修改了下, 用下面的替换原来的方法,其它的自己改了:
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(((long)Integer.parseInt(hexStr, 16))*1000);
System.out.print("Date: " + sdf.format(cal.getTime()) + "|" + hexStr.toUpperCase());
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
String hexStr = "";
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
hexStr = "0" + value;
else{
hexStr = Integer.toHexString(value&0xFF);
if(hexStr.length() == 1){
hexStr = "0" + hexStr ;
}
}
return hexStr;
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexParser {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/SUPERSTK.DAD";//Change it to the actual file path
public static void main(String[] args) {
try {
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = toStringHex(bytes[0]) + toStringHex(bytes[1])
+ toStringHex(bytes[2]) + toStringHex(bytes[3]);
if(hexStr.equalsIgnoreCase("FFFFFFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
in.close();
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 4);
getItemX(bytes, 4, 4, 5);
getItemX(bytes, 8, 4, 6);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
getItemX(bytes, 12, 4, 3);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(((long)Integer.parseInt(hexStr, 16))*1000);
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexEecodeAndDecodeDemo {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/data.dat";//Change it to the actual file path
public static void main(String[] args) {
try {
writeHexFile();
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = toStringHex(bytes[0]) + toStringHex(bytes[1])
+ toStringHex(bytes[2]) + toStringHex(bytes[3]);
if(hexStr.equalsIgnoreCase("FFFFFFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
in.close();
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 3);
getItemX(bytes, 4, 4, 4);
getItemX(bytes, 8, 4, 5);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(Integer.parseInt(hexStr, 16));
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
public static void writeHexFile() throws Exception{
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileFullPath));
writeInt(out);
writeInt(out);
writeInt(out);
writeInt(out);
out.writeInt(-1);
writeID("SH600000", out);
writeInt(out);
writeInt(out);
writeIDName("浦发银行", out);
writeInt(out);
writeDate(1396310400, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
writeDate(1396396800, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
out.writeInt(-1);
writeID("SH600001", out);
writeInt(out);
writeInt(out);
writeIDName("上海银行", out);
writeInt(out);
writeDate(1396310400, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
writeDate(1396396800, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
out.close();
}
private static void writeIDName(String IDName, DataOutputStream out) throws IOException {
byte[] bytes = IDName.getBytes(Charset.forName("GBK"));
for(int i=0; i<bytes.length; i++)
{
out.writeByte(bytes[i]);
}
}
private static void writeInt(DataOutputStream out)throws IOException{
out.writeInt(0);
}
private static void writeID(String ID, DataOutputStream out) throws IOException {
byte[] bytes = ID.getBytes();
for(int i = 0; i < bytes.length; i++)
{
out.writeByte(bytes[i]);
}
}
private static void writeDate(int milSeconds, DataOutputStream out)throws IOException{
reverseOrder(milSeconds, out);
}
private static void writeItemX(int itemX, DataOutputStream out)throws IOException{
reverseOrder(itemX, out);
}
private static void reverseOrder(int value, DataOutputStream out) throws IOException{
String hexStr = Integer.toHexString(value);
for(int i = hexStr.length(); i > 0; i -= 2 ){
out.write(Integer.valueOf(hexStr.substring(i-2, i), 16));
}
}
}
[/quote]
附件我上传了:http://download.csdn.net/detail/u014301433/7213471
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class HexEecodeAndDecodeDemo {
static Calendar cal = Calendar.getInstance();
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
static String fileFullPath = "E:/data.dat";//Change it to the actual file path
public static void main(String[] args) {
try {
writeHexFile();
readHexFile();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readHexFile()throws Exception{
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fileFullPath)));
in.skipBytes(16);
byte[] bytes = new byte[16];
int nCounts = 0;
while((in.read(bytes)) != -1){
String hexStr = toStringHex(bytes[0]) + toStringHex(bytes[1])
+ toStringHex(bytes[2]) + toStringHex(bytes[3]);
if(hexStr.equalsIgnoreCase("FFFFFFFF")){
nCounts = 1;
}else{
nCounts++;
}
if(nCounts > 2){
getDateAndItemx(bytes, nCounts);
}else{
getIDAndIDName(bytes, nCounts);
}
}
in.close();
}
private static void getIDAndIDName(byte[] bytes, int nCounts) {
if(nCounts == 1){
System.out.print("ID: " + new String(bytes, 4, 8));
System.out.print(", ");
}else{
System.out.print("IDName: " + new String(bytes, 4, 8, Charset.forName("GBK")));
System.out.print(": ");
System.out.println();
}
}
private static void getDateAndItemx(byte[] bytes, int nCounts) {
if(nCounts%2 == 0){
getItemX(bytes, 0, 4, 3);
getItemX(bytes, 4, 4, 4);
getItemX(bytes, 8, 4, 5);
System.out.println();
}else{
getDate(bytes);
getItemX(bytes, 4, 4, 1);
getItemX(bytes, 8, 4, 2);
}
}
private static String getItemX(byte[] bytes, int start, int length, int itemIndex) {
StringBuilder hexStr = new StringBuilder();
for(int i = start + length - 1 ; i >= start; i--){
hexStr.append(toStringHex(bytes[i]));
}
System.out.print("Item0" + itemIndex + ": " + hexStr.toString().toUpperCase());
System.out.print(", ");
return hexStr.toString();
}
private static String getDate(byte[] bytes){
String hexStr = toStringHex(bytes[3]) + toStringHex(bytes[2])
+ toStringHex(bytes[1]) + toStringHex(bytes[0]);
cal.setTimeInMillis(Integer.parseInt(hexStr, 16));
System.out.print("Date: " + sdf.format(cal.getTime()));
System.out.print(", ");
return hexStr;
}
public static String toStringHex(byte value) {
if(value < 0) value += 256;
if(value >= 0 && value <= 9)
return "0" + value;
else
return Integer.toHexString(value&0xFF);
}
public static void writeHexFile() throws Exception{
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileFullPath));
writeInt(out);
writeInt(out);
writeInt(out);
writeInt(out);
out.writeInt(-1);
writeID("SH600000", out);
writeInt(out);
writeInt(out);
writeIDName("浦发银行", out);
writeInt(out);
writeDate(1396310400, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
writeDate(1396396800, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
out.writeInt(-1);
writeID("SH600001", out);
writeInt(out);
writeInt(out);
writeIDName("上海银行", out);
writeInt(out);
writeDate(1396310400, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
writeDate(1396396800, out);
writeItemX(Integer.valueOf("411A8F5C", 16),out);
writeItemX(Integer.valueOf("411D70A4", 16),out);
writeItemX(Integer.valueOf("411A147B", 16),out);
writeItemX(Integer.valueOf("411BAE14", 16),out);
writeItemX(Integer.valueOf("4993E0F0", 16),out);
writeItemX(Integer.valueOf("418C69BA", 16),out);
writeInt(out);
out.close();
}
private static void writeIDName(String IDName, DataOutputStream out) throws IOException {
byte[] bytes = IDName.getBytes(Charset.forName("GBK"));
for(int i=0; i<bytes.length; i++)
{
out.writeByte(bytes[i]);
}
}
private static void writeInt(DataOutputStream out)throws IOException{
out.writeInt(0);
}
private static void writeID(String ID, DataOutputStream out) throws IOException {
byte[] bytes = ID.getBytes();
for(int i = 0; i < bytes.length; i++)
{
out.writeByte(bytes[i]);
}
}
private static void writeDate(int milSeconds, DataOutputStream out)throws IOException{
reverseOrder(milSeconds, out);
}
private static void writeItemX(int itemX, DataOutputStream out)throws IOException{
reverseOrder(itemX, out);
}
private static void reverseOrder(int value, DataOutputStream out) throws IOException{
String hexStr = Integer.toHexString(value);
for(int i = hexStr.length(); i > 0; i -= 2 ){
out.write(Integer.valueOf(hexStr.substring(i-2, i), 16));
}
}
}
#include<stdio.h>
#include <windows.h>
unsigned char ReadByte(char* szFilePath,int noffset){
HANDLE hHexFile=CreateFile(
szFilePath, //被操作的资源名称
GENERIC_READ|GENERIC_WRITE, //对资源的读写操作权限
FILE_SHARE_READ|FILE_SHARE_WRITE,//共享模式
0, //指向安全属性的指针
OPEN_EXISTING, // 如何操作,这里是打开存在的资源
FILE_ATTRIBUTE_NORMAL, // 文件属性
NULL //如果不为零,则指定一个文件句柄,新文件将从这个文件中复制扩展属性
);
LARGE_INTEGER lDistance; //LARGE_INTEGER结构体,用于SetFilePointer
unsigned char buf[1];
lDistance.QuadPart=noffset*1;//指针距离
DWORD dwi; //用于ReadFile作参数
//设置指针移动位置,FILE_BEGIN代表从这个资源的开始位置往后的偏移
SetFilePointer(hHexFile,lDistance.LowPart,&lDistance.HighPart,FILE_BEGIN);
//将数据读入buf
ReadFile(hHexFile,buf,1,&dwi,NULL);
CloseHandle(hHexFile);
//得到指定位置的字节,并返回
unsigned char byNum;
byNum=buf[0];
return byNum;
}
int main(void)
{
for (int noffset = 0; noffset < 10 ; noffset++)
{
unsigned char byRes=ReadByte("D:\\HexFile.dat",noffset);
printf("第%d个字节为:%02X \n",noffset,byRes);
}
return 0;
}