十六进制字符串转换成图片

十六进制字符串转换成图片(十六进制数据位于文件中)



import java.io.*;

public class MyFontImage {
    
    

    public static void main(String[] args) throws Exception {
    
    
        System.out.println(readTxt().replaceAll(" ", ""));
        txtTransImage(readTxt().replaceAll(" ", "").toUpperCase(), "d:/image.png");

    }


    private static String readTxt() throws Exception {
    
    
        Reader inputStream = new FileReader("d:/image.txt");
        BufferedReader reader = new BufferedReader(inputStream);
        String str = "";
        StringBuilder builder = new StringBuilder();
        while ((str = reader.readLine()) != null) {
    
    
            builder.append(str);
        }
        return builder.toString();
    }

    private static void txtTransImage(String src, String filePath) throws Exception {
    
    
        if (src == null || ("").equals(src)) {
    
    
            return;
        }
        FileOutputStream outputStream = new FileOutputStream(filePath);
        byte[] bytes = src.getBytes();
        for (int i = 0; i < bytes.length; i += 2) {
    
    
            outputStream.write(charToInt(bytes[i]) * 16 + charToInt(bytes[i + 1]));
        }
        outputStream.close();
    }

    private static int charToInt(byte ch) {
    
    
        int val = 0;
        if (ch >= 0x30 && ch <= 0x39) {
    
    
            val = ch - 0x30;
        } else if (ch >= 0x41 && ch <= 0x46) {
    
    
            val = ch - 0x41 + 10;
        }
        return val;
    }


}

imgae.txt中存放十六进制数据,部分数据截图如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45170656/article/details/107052859