Java:文件到十六进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1314568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: File to Hex?
提问by BinRoot
I have a file in Java
我有一个 Java 文件
FileInputStream in = null;
try{
in = new FileInputStream("C:\pic.bmp");
}catch{}
I want to convert pic.bmpto an array of hex values so I can edit and save it as a modified version.
我想将pic.bmp转换为十六进制值数组,以便我可以编辑并将其保存为修改后的版本。
Is there a java class to do this?
有没有一个java类来做到这一点?
回答by Brent Writes Code
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.
你很幸运。几个月前我不得不这样做。这是一个从命令行获取两个参数的简化版本。两个命令行参数都是文件名……第一个是输入文件,第二个是输出文件。输入文件以二进制形式读取,输出文件以 ASCII 十六进制写入。希望您可以根据自己的需要进行调整。
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();
}
}
}
回答by David Z
Java has an extensive image reading/writing and editing library. Look at the javax.imageiopackages (here's the documentation). You would probably want to create a BufferedImageusing ImageIOand then access the image data from the BufferedImageobject (there are methods for that).
Java 有一个广泛的图像读/写和编辑库。查看javax.imageio包(这里是文档)。您可能想要创建一个BufferedImageusingImageIO然后从BufferedImage对象访问图像数据(有一些方法)。
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);
回答by Lispnik
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.htmlwhich is what you should use to answer the "I want to convert pic.bmp to an array of hex values" part of your question.
如果你输入“java的十六进制编码”成一个谷歌搜索,第一个结果是http://commons.apache.org/codec/api-release/org/apache/commons/codec/binary/Hex.html这就是你应该用于回答问题的“我想将 pic.bmp 转换为十六进制值数组”部分。
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
不过,我不知道它如何帮助您“因此我可以编辑并将其另存为修改后的版本”。您可能应该为此使用十六进制编辑器。例如。ghex2
回答by Ken
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.
如果你想自己摆弄字节,从 FileInputStream 中获取一个 FileChannel,然后分配一个 ByteBuffer,然后将所有内容读入其中。ByteBuffer 还具有以两种不同字节顺序处理较大字节块的方法。

