Home > Back-end >  Is there a way to encode data from Windows-1253 encoding to ISO-8859-1 in java?
Is there a way to encode data from Windows-1253 encoding to ISO-8859-1 in java?

Time:11-22

By mistake I encoded hex data with Windows-1253 (Java eclipse option) but the data should be encoded with ISO-8859-1. Is there a way to re encode the data to get the right conversion?

CodePudding user response:

If your version of Java supports Windows-1253 (mine does), then yes. You can check with Charset.isSupported.

Re-encoding:

void encode( File src, File tgt ) throws IOException  {
  if (Charset.isSupported("Windows-1253")) {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(src), "Windows-1253"))) {
      try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tgt), "ISO-8859-1"))) {
        String del = "";
        for (String line = br.readLine(); line != null; line = br.readLine()) {
          bw.write(del);
          bw.write(line);
          del = "\r\n";
        }

        bw.flush();
      }
    }
  } else {
    throw new IOException("Unsupported character encoding: Windows-1253");
  }
}

(Not tested.)

CodePudding user response:

Thank you very much for the reply. I tried it but the source and the target appear to be identical...The data file is 512 integers. Each integer is produced by 3 bytes of hexadecimals... from the following formula: int r = (b3 & 0xFF) | ((b2 & 0xFF) << 8) | ((b1 & 0x0F) << 16); I dont have the hexadecimals but just the integers that were produced...

  • Related