相关文章推荐
读研的人字拖  ·  Java 读取 .properties ...·  4 天前    · 
犯傻的绿茶  ·  clickhouse ...·  2 天前    · 
老实的玉米  ·  springboot+kafka中@Kafk ...·  2 天前    · 
讲道义的大海  ·  ASP.NET Core の Razor ...·  昨天    · 
胡子拉碴的大葱  ·  matlab ...·  1 年前    · 
光明磊落的镜子  ·  [转](SQL Server) ...·  1 年前    · 
爱健身的镜子  ·  python - ...·  1 年前    · 
风流倜傥的大蒜  ·  errno, _doserrno, ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

That would be something like as follows:

public static String fromCharCode(int... codePoints) {
    StringBuilder builder = new StringBuilder(codePoints.length);
    for (int codePoint : codePoints) {
        builder.append(Character.toChars(codePoint));
    return builder.toString();

Note that casting to char isn't guaranteed to work because the codepoint value might exceed the upper limit of char (65535). The char was established in the dark Java ages when Unicode 3.1 wasn't out yet which goes beyond 65535 characters.

Update: the String has actually a constructor taking an int[] (introduced since Java 1.5, didn't knew it from top of head), which handles this issue correctly. The above could be simplified as follows:

public static String fromCharCode(int... codePoints) {
    return new String(codePoints, 0, codePoints.length);
                +1 for Unicode perfection, but I doubt the Javascript function can correctly handle codepoints bigger than 65536.
– President James K. Polk-Strike
                May 31, 2010 at 21:44
                This appears to work for most values.  I am trying: new String(new int[]{141}, 0, 1) which in javascript prints, "?" but in java does not.  Is 141 invalid?  I am trying to debug some obfuscation code I ported from Javascript to Java and am getting inconsistent results for some characters.
– jon077
                Jun 1, 2010 at 1:52
                That's dependent on the character encoding which is a completely different story. The JS environment is apparenly using a different character encoding than Java environment. At least, the above should work flawlessly for an Unicode character encoding like UTF-8.
– BalusC
                Jun 1, 2010 at 2:05
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.