简体   繁体   English

在java中打印unicode字符

[英]Print unicode character in java

Displaying unicode character in java shows "?"在java中显示unicode字符显示“?” sign.符号。 For example, i tried to print "अ".例如,我尝试打印“अ”。 Its unicode Number is U+0905 and html representation is "अ".它的unicode编号是U+0905,html表示是“अ”。 The below codes prints "?"下面的代码打印“?” instead of unicode character.而不是unicode字符。

char aa = '\u0905';
String myString = aa + " result" ;
System.out.println(myString); // displays "? result"

Is there a way to display unicode character directly from unicode itself without using unicode numbers?有没有办法直接从unicode本身显示unicode字符而不使用unicode数字? ie "अ" is saved in file now display the file in jsp.即“अ”保存在文件中,现在显示 jsp 中的文件。

Java defines two types of streams, byte and character. Java 定义了两种类型的流,字节和字符。

The main reason why System.out.println() can't show Unicode characters is that System.out.println() is a byte stream that deal with only the low-order eight bits of character which is 16-bits. System.out.println() 不能显示Unicode 字符的主要原因是System.out.println() 是一个字节流,只处理16 位字符的低8 位。

In order to deal with Unicode characters(16-bit Unicode character), you have to use character based stream ie PrintWriter.为了处理 Unicode 字符(16 位 Unicode 字符),您必须使用基于字符的流,即 PrintWriter。

PrintWriter supports the print( ) and println( ) methods. PrintWriter 支持print() 和println() 方法。 Thus, you can use these methods in the same way as you used them with System.out.因此,您可以像使用 System.out 一样使用这些方法。

PrintWriter printWriter = new PrintWriter(System.out,true);
char aa = '\u0905';
printWriter.println("aa = " + aa);

try to use utf8 character set -尝试使用 utf8 字符集 -

        Charset utf8 = Charset.forName("UTF-8");
        Charset def = Charset.defaultCharset();

        String charToPrint = "u0905";

        byte[] bytes = charToPrint.getBytes("UTF-8");
        String message = new String(bytes , def.name());

        PrintStream printStream = new PrintStream(System.out, true, utf8.name());
        printStream.println(message); // should print your character

Your myString variable contains the perfectly correct value.您的myString变量包含完全正确的值。 The problem must be the output from System.out.println(myString) which has to send some bytes to some output to show the glyphs that you want to see.问题必须是System.out.println(myString)的输出,它必须将一些字节发送到某些输出以显示您想要查看的字形。

System.out is a PrintStream using the "platform default encoding" to convert characters to byte sequences - maybe your platform doesn't support that character. System.out是使用“平台默认编码”将字符转换为字节序列的 PrintStream - 也许您的平台不支持该字符。 Eg on my Windows 7 computer in Germany, the default encoding is CP1252, and there's no byte sequence in this encoding that corresponds to your character.例如,在我在德国的 Windows 7 计算机上,默认编码是 CP1252,并且此编码中没有与您的字符对应的字节序列。

Or maybe the encoding is correct, but simply the font that creates graphical glyphs from characters doesn't have that charater.或者也许编码是正确的,但只是从字符创建图形字形的字体没有那个字符。

If you are sending your output to a Windows CMD.EXE window, then maybe both reasons apply.如果您将输出发送到 Windows CMD.EXE 窗口,那么可能这两个原因都适用。

But be assured, your string is correct, and if you send it to a destination that can handle it (eg a Swing JTextField), it'll show up correctly.但请放心,您的字符串是正确的,如果您将其发送到可以处理它的目的地(例如 Swing JTextField),它将正确显示。

Unicode is a unique code which is used to print any character or symbol. Unicode 是用于打印任何字符或符号的唯一代码。

You can use unicode from --> https://unicode-table.com/en/您可以使用来自 --> https://unicode-table.com/en/ 的 unicode

Below is an example for printing a symbol in Java.下面是在 Java 中打印符号的示例。

        package Basics;
        
        /**
         *
         * @author shelc
         */
        public class StringUnicode {
        
            public static void main(String[] args) {
        
                String var1 = "Cyntia";
                String var2 = new String(" is my daughter!");
        
                System.out.println(var1 + " \u263A" + var2);
    
                //printing heart using unicode
                System.out.println("Hello World \u2665");
        
            }
        
        }
        
    ******************************************************************
    OUTPUT-->
    
    Cyntia ☺ is my daughter!
    Hello World ♥
          

I ran into the same problem wiht Eclipse.我在 Eclipse 中遇到了同样的问题。 I solved my problem by switching the Encoding format for the console from ISO-8859-1 to UTF-8.我通过将控制台的编码格式从 ISO-8859-1 切换到 UTF-8 解决了我的问题。 You can do in the Run/Run Configurations/Common menu.您可以在 Run/Run Configurations/Common 菜单中进行。

https://eclipsesource.com/blogs/2013/02/21/pro-tip-unicode-characters-in-the-eclipse-console/ https://eclipsesource.com/blogs/2013/02/21/pro-tip-unicode-characters-in-the-eclipse-console/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM