1

以下代码??????作为输出返回,当str有阿拉伯字符串时:

String str="مرحبا",str2="";
for (int i = 0; i < str.length(); ++i) {
                str2 += displayChar(str.charAt(reorder[i]));
                System.out.print(reorder[i]);
            }
   System.out.println(str2); // output is : ?????

和 :

String displayChar(char c) {
        if (c < '\u0010') {
            return "0x0" + Integer.toHexString(c);
        } else if (c < '\u0020' || c >= '\u007f') {
            return "0x" + Integer.toHexString(c);
        } else {
            return c+"";
        }
    }

For reorderisinteger数组只携带给定字符的新索引(顺序)str

Here is the complete code, .. hope it will help you to understand the problem :
/*
 * (C) Copyright IBM Corp. 1999, All Rights Reserved
 *
 * version 1.0
 */

import java.io.*;

/**
 * A simple command-line interface to the BidiReference class.
 * <p>
 * This prompts the user for an ASCII string, runs the reference
 * algorithm on the string, and displays the results to the terminal.
 * An empty return to the prompt exits the program.
 * <p>
 * ASCII characters are preassigned various bidi direction types. 
 * These types can be displayed by the user for reference by
 * typing <code>-display</code> at the prompt.  More help can be
 * obtained by typing <code>-help</code> at the prompt.
 */
public class BidiReferenceTest {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
    BidiReferenceTestCharmap charmap = BidiReferenceTestCharmap.TEST_ARABIC;
    byte baseDirection = -1;

    /**
     * Run the interactive test.
     */
    public static void main(String args[]) {
        new BidiReferenceTest().run();
    }

    void run() {
        //printHelp();

        while (true) {
            writer.print("> ");
            writer.flush();
            String input;
            try {
                input = reader.readLine();
            }
            catch (Exception e) {
                writer.println(e);
                continue;
            }

            if (input.length() == 0) {
                writer.println("Bye!");
                writer.flush();
                return;
            }

            if (input.charAt(0) == '-') { // command
                int limit = input.indexOf(' ');
                if (limit == -1) {
                    limit = input.length();
                }
                String cmd = input.substring(0, limit);
                if (cmd.equals("-display")) {
                    charmap.dumpInfo(writer);
                } else if (cmd.equals("-english")) {
                    charmap = BidiReferenceTestCharmap.TEST_ENGLISH;
                    charmap.dumpInfo(writer);
                } else if (cmd.equals("-hebrew")) {
                    charmap = BidiReferenceTestCharmap.TEST_HEBREW;
                    charmap.dumpInfo(writer);
                } else if (cmd.equals("-arabic")) {
                    charmap = BidiReferenceTestCharmap.TEST_ARABIC;
                    charmap.dumpInfo(writer);
                } else if (cmd.equals("-mixed")) {
                    charmap = BidiReferenceTestCharmap.TEST_MIXED;
                    charmap.dumpInfo(writer);
                } else if (cmd.equals("-baseLTR")) {
                    baseDirection = 0;
                } else if (cmd.equals("-baseRTL")) {
                    baseDirection = 1;
                } else if (cmd.equals("-baseDefault")) {
                    baseDirection = -1;
                } else {
                }
            } else {

                String ss= runSample(input);
                System.out.println(ss);
                Character.UnicodeBlock block =  Character.UnicodeBlock.of(Character.codePointAt(ss, 0));

            }
        }
    }



    String runSample(String str) {
        String str2 = "";
        try {
            charmap = BidiReferenceTestCharmap.TEST_ARABIC;

            byte[] codes = charmap.getCodes(str);
            baseDirection = 1;
            BidiReference bidi = new BidiReference(codes, baseDirection); // baseDirection = 1
            int[] reorder = bidi.getReordering(new int[] { codes.length });
            /*
            writer.println("base level: " + bidi.getBaseLevel() + (baseDirection != -1 ? " (forced)" : ""));

            // output original text
            for (int i = 0; i < str.length(); ++i) {
                displayChar(str.charAt(i));
            }
            writer.println();
             */
            // output visually ordered text
            for (int i = 0; i < str.length(); ++i) {
                str2 += displayChar(str.charAt(reorder[i]));
                System.out.print(reorder[i]);
            }
            return str2;
        }
        catch (Exception e) {
            return "";
        }
    }

    String displayChar(char c) {
        if (c < '\u0010') {
            return "0x0" + Integer.toHexString(c);
        } else if (c < '\u0020' || c >= '\u007f') {
            return "0x" + Integer.toHexString(c);
        } else {
            return c+"";
        }
    }
}
4

2 回答 2

0

一个问题是您的终端可能不正确支持 Unicode 字符(这可能不是唯一的问题)。

于 2012-08-04T18:32:16.277 回答
0

如果我猜的话,我会说您在 Windows 下使用默认控制台设置(即光栅字体)运行,并且您从控制台而不是在 Eclipse 中运行 Java 程序。

如果是这种情况,那么只需将控制台设置更改为使用 TrueType 字体(Lucida Console 或 Consolas),您应该会看到方框而不是问号。这些看起来也不对,但至少它是实际文本而不是问号。

旁注:如果某些东西确实支持 Unicode,但在某处将其转换为另一种编码,例如拉丁语 1,问号是很常见的。

于 2012-08-04T18:34:27.480 回答