简体   繁体   English

在Java中以十六进制读写文件?

[英]Reading and writing a file in java in hexadecimal?

I'm having trouble finding what I'm looking for. 我在寻找所需内容时遇到了麻烦。 Is there anyway to read a file by hexadecimal values in java, and to write another file? 无论如何,是否可以通过Java中的十六进制值读取文件并写入另一个文件? If I wanted to take one file and create a new file where every hexadecimal value was incremented, how would I do this? 如果我想取一个文件并创建一个新文件,每个十六进制值都会增加,我该怎么做? Like if I had a txt file that said "Hello" and I increment every hexadecimal value so that it should say "Ifmmp". 就像我有一个说“ Hello”的txt文件一样,我增加了每个十六进制值,因此它应该说“ Ifmmp”。

How do I read a file (any file, not just an ASCII text file) hexadecimal by hexadecimal, and write another file one hexadecimal at a time? 如何以十六进制读取文件(任何文件,而不仅仅是ASCII文本文件),然后一次写入另一个十六进制的文件?

I believe this is what you're looking for... 我相信这就是您要寻找的...

Here's code to read from a file and take the hex values of each part in file. 这是从文件读取并获取文件中每个部分的十六进制值的代码。

/*
    Code which defines a scanner class and FileInputStream
*/
String lineInFile = scannerName.nextLine();
int[] convertMeToHex = new int[lineInFile.length()];

for (int i = 0; i < convertMeToHex.length; i++)
    convertMeToHex[i] = (int) lineInFile.charAt(i);

String[] hex = new String[convertMeToHex.length];

for (int i = 0; i < convertMeToHex.length; i++)
    hex[i] = Integer.toHexString(convertMeToHex[i]));

You can convert hex back to int with int hexToInt = Integer.parseInt(hexNumber, 16); 您可以使用int hexToInt = Integer.parseInt(hexNumber, 16);将十六进制转换回int hexToInt = Integer.parseInt(hexNumber, 16);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM