I am trying to read data from an mp3 file so that they can later be manipulated as hexadecimals. Suppose if I opened an mp3 file in a text editor and I see the characters ÿû²d. The translation should read FF FB B2 64 in hexadecimal (indicating a header). However, the Hex that appears in the output text file is 6E 75 6C 6C and I cannot figure out why.
Sources:
Java code To convert byte to Hexadecimal
convert audio,mp3 file to string and vice versa
How to check the charset of string in Java?
My code:
package mp3ToHex;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.*;
public class mp3ToHex {
  public static void main(String[] args) {
    //directories
    String fileIn = "Some\\Input\\Directory.mp3", fileOut = "Some\\Output\\Directory.txt";
    outputData(fileOut, fileIn);
  }
  @SuppressWarnings("unused")
  public static String readFile(String filename) {
    // variable representing a line of data in the mp3 file
    String line = "";
    try {
      BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
      while (br.readLine() != null) {
        line += br.readLine();
        try {
          if (br == null) {
            // close reader when all data is read
            br.close();
          }
        } catch (FileNotFoundException e) {
          e.getMessage();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    } catch (FileNotFoundException e) {
      e.getMessage();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return line;
  }
  public static void outputData(String outputFile, String inputFile) {
    try {
      // Create file
      FileWriter fileStream = new FileWriter(outputFile);
      BufferedWriter writer = new BufferedWriter(fileStream);
      // Convert string to hexadecimal
      String output = toHex(readFile(inputFile));
      StringBuilder s = new StringBuilder();
      for (int i = 0; i < output.length(); i++) {
        // Format for easier reading
        if (i % 64 == 0) s.append('\n');
        else if (i % 2 == 0) s.append(' ');
        s.append(output.charAt(i));
      }
      // Write to file
      writer.write(s.toString());
      // Close writer
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  // Converts strings to hexadecimal
  public static String toHex(String arg) throws UnsupportedEncodingException {
    return String.format("%02X", new BigInteger(1, arg.getBytes(charset(arg, new String[] {
      "US-ASCII",
      "ISO-8859-1",
      "UTF-8",
      "UTF-16BE",
      "UTF-16LE",
      "UTF-16"
    }))));
  }
  // Converts strings to different encodings
  public static String convert(String value, String fromEncoding, String toEncoding) throws UnsupportedEncodingException {
    return new String(value.getBytes(fromEncoding), toEncoding);
  }
  // Detects which Charset a string is encoded in by decoding and re-encoding a string. The correct encoding is found if the transformation yields no changes.
  public static String charset(String value, String charsets[]) throws UnsupportedEncodingException {
    String probe = StandardCharsets.UTF_8.name();
    for (String c: charsets) {
      Charset charset = Charset.forName(c);
      if (charset != null) {
        if (value.equals(convert(convert(value, charset.name(), probe), probe, charset.name()))) {
          return c;
        }
      }
    }
    return StandardCharsets.UTF_8.name();
  }
}