In JAVA the whole sentence Chinese character is split and converted into ASCII

  • 2020-06-19 10:14:59
  • OfStack

In JAVA, the whole sentence Chinese character is split and converted into ASCII

As we all know, one Chinese character is the size of two byte. When base 2 data is transmitted over the network, if both byte exceed 128, it will be merged into one Unicode(Chinese character) character. The main function of the code in this paper is to split these characters into byte and then change them back into ASCII type string.


public static String ChineseToASCII(byte[] rec) { // Reads content from a byte 
   ByteArrayInputStream bais = new ByteArrayInputStream(rec);
   DataInputStream dis = new DataInputStream(bais);
   String BTS=null;
   try {
   BTS=new String(rec,"ISO8859-1");// Transform coding 
   bais.close();
   dis.close();
   } catch (Exception e) {
   e.printStackTrace();
   }
   return BTS;
 }
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    String source="12345678910";
    System.out.println(source.length());
    String target=ChineseToASCII(source.getBytes());
    System.out.println(target);
    System.out.println(target.length());
  }

The result is:


compile:
run:
10
??????????? u ?? ° ????? ASCII If the character exceeds 128 , will be shown as? But its value doesn't change 
20
BUILD SUCCESSFUL (total time: 1 second)

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: