how do I read any file like images , videos , text Files .... etc
as an hex String
then write these String
again in other place but with ability to open file as original
I'm looking for a code ??
Thanks in advance
how do I read any file like images , videos , text Files .... etc
as an hex String
then write these String
again in other place but with ability to open file as original
I'm looking for a code ??
Thanks in advance
Read File into Array of bytes and convert it to Hex
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
result.toString();
Make any modification to hex then save it again using this method
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
This Method return an Array of byte
so you can write these bytes using this code
RandomAccessFile file = new RandomAccessFile([Any file Name with extention], "rw");
file.write(bytes);
file.close();
use the format() method.
Read your file as bytes and then convert it to bytes.
String.format("%02x", byteFromFile);
Iterate it until all bytes are read and then add it to a StringBuilder
StringBuilder hexbyteString;
for(byte myfile : filebyte) {
hexbyteString.append(String.format("%02x", myfile));
}
return hexbyteString.toString();