Java: File to Hex?

后端 未结 4 1509
别跟我提以往
别跟我提以往 2020-12-16 19:37

I have a file in Java

FileInputStream in = null;
try{    
in = new FileInputStream(\"C:\\\\pic.bmp\");
}catch{}

I want to convert pic.b

相关标签:
4条回答
  • 2020-12-16 20:24

    Java has an extensive image reading/writing and editing library. Look at the javax.imageio packages (here's the documentation). You would probably want to create a BufferedImage using ImageIO and then access the image data from the BufferedImage object (there are methods for that).

    If you want a generic answer, for any type of binary data (not just images), then I guess you would have to read the contents of the file into a byte array. Something like this:

    byte[] bytes = new byte[in.available()];
    in.read(bytes);
    
    0 讨论(0)
  • 2020-12-16 20:26

    You're in luck. I had to do this a couple months ago. Here's a dumbed-down version that takes two parameters from the command line. Both comand line parameters are filenames...the first is the input file and the second is the output file. The input file is read in binary and the output file is written as ASCII hex. Hopefully you can just adapt this to your needs.

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class BinToHex
    {
        private final static String[] hexSymbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    
        public final static int BITS_PER_HEX_DIGIT = 4;
    
        public static String toHexFromByte(final byte b)
        {
            byte leftSymbol = (byte)((b >>> BITS_PER_HEX_DIGIT) & 0x0f);
            byte rightSymbol = (byte)(b & 0x0f);
    
            return (hexSymbols[leftSymbol] + hexSymbols[rightSymbol]);
        }
    
        public static String toHexFromBytes(final byte[] bytes)
        {
            if(bytes == null || bytes.length == 0)
            {
                return ("");
            }
    
            // there are 2 hex digits per byte
            StringBuilder hexBuffer = new StringBuilder(bytes.length * 2);
    
            // for each byte, convert it to hex and append it to the buffer
            for(int i = 0; i < bytes.length; i++)
            {
                hexBuffer.append(toHexFromByte(bytes[i]));
            }
    
            return (hexBuffer.toString());
        }
    
        public static void main(final String[] args) throws IOException
        {
            try
            {
                FileInputStream fis = new FileInputStream(new File(args[0]));
                BufferedWriter fos = new BufferedWriter(new FileWriter(new File(args[1])));
    
                byte[] bytes = new byte[800];
                int value = 0;
                do
                {
                    value = fis.read(bytes);
                    fos.write(toHexFromBytes(bytes));
    
                }while(value != -1);
    
                fos.flush();
                fos.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:28

    If you want to fiddle with the bytes yourself, get a FileChannel from the FileInputStream, and then allocate a ByteBuffer and then read all the content into it. ByteBuffer also has methods to deal with larger chunks of bytes, in the two different byte orders.

    0 讨论(0)
  • 2020-12-16 20:35

    If you type "java hexidecimal encoding" into a Google search, the first result is http://commons.apache.org/codec/api-release/org/apache/commons/codec/binary/Hex.html which is what you should use to answer the "I want to convert pic.bmp to an array of hex values" part of your question.

    I don't see how it helps you with "so I can edit and save it as a modified version" though. You should probably use a hexidecimal editor for that. eg. ghex2

    0 讨论(0)
提交回复
热议问题