Use Java to parse the correct text from the garbled text

  • 2020-04-01 03:15:00
  • OfStack

This is a program that can get the correct raw text from the scrambled text, based on the principle that bad coding often leads to bit supplementation, so the correct text should use the least number of bytes.



package com.hongyuan.test;
import java.io.UnsupportedEncodingException;

public class CharSetTest {
 public static final String[] CHARSET_NAMES=new String[]{"ISO8859-1","GBK","UTF-8"};

 public static void main(String[] args) throws UnsupportedEncodingException {
  //Garbled string
  String str=" Fear and fear of harm Windows XP They can be used in all manner of ways ";

  int strLength=Integer.MAX_VALUE; //Characters in length
  String newStr="";     // from Garbled string The analyzed string 
  String srcCharSet="";    // The current Garbled string coding 
  String targetCharSet="";   //Garbled string Correct coding 

  //The possible code combinations are traversed, from which the code format with the smallest code length is derived
  for(int i=0;i<CHARSET_NAMES.length;i++){
   for(int j=0;j<CHARSET_NAMES.length;j++){
    String temp=new String(str.getBytes(CHARSET_NAMES[i]),CHARSET_NAMES[j]);
    //System.out.println(temp);
    if(temp.length()<=strLength){
     strLength=temp.length();
     newStr=temp;
     srcCharSet=CHARSET_NAMES[i];
     targetCharSet=CHARSET_NAMES[j];
    }
   }
  }
  //Output the queried encoding and the correct text format
  System.out.println(srcCharSet+"-->"+targetCharSet+":"+newStr);
 }
}


Related articles: