Check if a String contains encoded characters

前端 未结 6 1039
情深已故
情深已故 2020-12-18 08:46

Hello I am looking for a way to detect if a string has being encoded

For example

    String name = \"Hellä world\";
    String encoded = new String(n         


        
6条回答
  •  旧巷少年郎
    2020-12-18 09:15

    String name = "Hellä world";
    String encoded = new String(name.getBytes("utf-8"), "iso8859-1");
    

    This code is just a character corruption bug. You take a UTF-16 string, transcode it to UTF-8, pretend it is ISO-8859-1 and transcode it back to UTF-16, resulting in incorrectly encoded characters.

提交回复
热议问题