十六进制字符串jpg图片互转
十六制字符串jpg图片互转(格式:FFD8FFE000104A******)如:FFD8FFE000104A46494600010100000100010000FFDB004300080606070605080707070909080A0C140D0C0B0B0C1912130F141D1A1F1E1D1A1C1C20242E2720222C231C1C2837292C30313434341F27393D38323C2E333432FFDB0043010909090C0B0C180D0D1832211C213232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232323232FFC00011080058004803012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00F7FA28ACBF10EB70787B42BAD52E3958532AB9FBEC780BF89A0076B5AFE9BE1FB2375A95CA429D141E59CFA01DEBCBB57F8DE03BC7A569C02F204970724FFC0474FCEBCB75DF126A5E28D61AE6EE4696673848D47CA83B2A8F4ABD65E10BAB840D3C8133D54734DB8C17BC5C61296C761A77C64D7448EF3C56D708173B0A6C23DC115B5A77C6E8E4942EA1A4948C9E5E193240FA11CFE75CB5AF85ADADEDCA6D62586093DEB1B54F0D35B8325B6463F84F4A855A0DD8D1D092573E8CD17C41A5F886D3ED1A6DDA4CA3EF28E193D88EA2B4EBE4CD175ED43C35ACC57B67214950F2A7A38EEAC3D2BEA0F0FEB50788743B5D52D81093A64A9EAAC3823F020D5B5630B1A745145200AF1DF8EDAD186CB4ED2236E642D3C833D870BFCDBF2AF62AF9D3E3679F3F8FA289C7C9F668D63E3B64E7F526AA3B8199E0FD26331FDA1973237F11EDF4AF44B28220A0605731A7E9EF15A476EB2F948AA0161D49ADFD3AD16D0EE5B992523AEF6CD724DA93BB67A14D5959235CC098FBB9ACCBF8E120AB0007BD6B971E56E2783E958375636524C64316641DC3722A2CBA9A4AFD0F3DF1669CB6D3F9E83E53DC57ABFC10D4C5CF866F2C5*********
/**
* 十六进制字节转字节数组
* Creates byte array representation of HEX string.<br>
* http://www.cnblogs.com/qgc88
* @param s
* string to parse
* @return
*/
public static byte[] fromHexString(String s) {
int length = s.length() / 2;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = (byte) ((Character.digit(s.charAt(i * 2), 16) << 4) | Character
.digit(s.charAt((i * 2) + 1), 16));
}
return bytes;
}
/**
* Creates HEX String representation of supplied byte array.<br/>
* Each byte is represented by a double character element from 00 to ff
* 实现字节数组向十六进制的转换方法一 http://www.cnblogs.com/qgc88
* @param fieldData
* to be tringed
* @return
*/
public static String toHexString(byte[] fieldData) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < fieldData.length; i++) {
int v = (fieldData[i] & 0xFF);
if (v <= 0xF) {
sb.append("0");
}
sb.append(Integer.toHexString(v));
}
return sb.toString();
}
//byte数组转图片 http://www.cnblogs.com/qgc88
public static void byte2image(byte[] data,String path){
if(data.length<3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
public static void main(String[] args) {
String str2="ffd8ffe000104a46494600010100000100010000f****ee3fdf4ff00d0857927f0cbf43fccd483";
byte[] t=fromHexString(str2);
byte2image(t, "d://1.jpg");//转成保存图片
/********************图片转十六进制******************************/
try
{
StringBuffer sb = new StringBuffer();
FileInputStream fis = new FileInputStream("d:/1.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff)) != -1)
{
bos.write(buff, 0, len);
}
// 得到图片的字节数组
byte[] result = bos.toByteArray();
System.out.println("++++" + toHexString(result));
// 字节数组转成十六进制
String str = toHexString(result);
//String str = byte2HexStr(result);
/* http://www.cnblogs.com/qgc88
* 将十六进制串保存到txt文件中
*/
PrintWriter pw = new PrintWriter(
new FileWriter("qgc88.txt"));
pw.println(str);
pw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}