简体   繁体   中英

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? 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".

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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