`
yiqieanhao
  • 浏览: 66066 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

文件,byte[],HexString 之间的转换

    博客分类:
  • JAVA
阅读更多
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * 文件,byte[],HexString 之间的转换
 *
 */
public class FileDataConvert {

	public static void main(String[] args) {
		String srcFilePath = "d:/testFrom.png";
		String outFilePath = "d:/";
		String outFileName = "testTo.png";

		byte[] bytes = getBytesFromFile(srcFilePath);
		String str = bytes2HexString(bytes);
		System.out.println(str);

		byte[] bytes2 = hexString2Bytes(str);
		saveBytes2File(bytes2, outFilePath, outFileName);
	}
	
	/**
	 * 从文件中获取byte数组
	 */
	public static byte[] getBytesFromFile(String filePath) {
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	/**
	 * 根据byte数组生成文件
	 */
	public static void saveBytes2File(byte[] bfile, String filePath, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(filePath);
			if (!dir.exists() && dir.isDirectory()) {
				dir.mkdirs();
			}
			file = new File(filePath + "\\" + fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bfile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	/**
	 * 从字节数组到十六进制字符串转换
	 */
	public static String bytes2HexString(byte[] b) {
		byte[] buff = new byte[2 * b.length];
		for (int i = 0; i < b.length; i++) {
			buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[b[i] & 0x0f];
		}
		return new String(buff);
	}

	/**
	 * 从十六进制字符串到字节数组转换
	 */
	public static byte[] hexString2Bytes(String hexstr) {
		byte[] b = new byte[hexstr.length() / 2];
		int j = 0;
		for (int i = 0; i < b.length; i++) {
			char c0 = hexstr.charAt(j++);
			char c1 = hexstr.charAt(j++);
			b[i] = (byte) ((parse(c0) << 4) | parse(c1));
		}
		return b;
	}

	private final static byte[] hex = "0123456789ABCDEF".getBytes();

	private static int parse(char c) {
		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;
	}
}
分享到:
评论
2 楼 yiqieanhao 2013-01-08  
fj1052 写道
parse方法中的"c >= 'a'"和"c - 'a' + 10"什么意思啊,运算我能看懂,就是不知道干什么?或者说,为什么要这么做

byte[]变hex字符串其实就是把byte[]里的每个byte变成2位的16进制的数值表示。把byte的高四位和低四位用"0123456789ABCDEF"来表示。
当反转时,取两位16进制数值变成byte,中间量为char.第一位16进制数值变成高四位,第二位16进制数值变成低四位。
int parse(char c) 就是将16进制数值char变成byte高4位或低4位的数值。(c - 'a' + 10) 是取得字符对应的值,& 0x0f是取后四位。"c >= 'a'"无非是判断以合适计算对应的值。
1 楼 fj1052 2013-01-08  
parse方法中的"c >= 'a'"和"c - 'a' + 10"什么意思啊,运算我能看懂,就是不知道干什么?或者说,为什么要这么做

相关推荐

Global site tag (gtag.js) - Google Analytics