1

次のコードは、アラビア語の文字列が??????ある場合、 output として返されます。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+"";
        }
    }

reorder配列はinteger、指定された文字の新しいインデックス (順序) のみを保持します。 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

問題の 1 つは、お使いの端末が Unicode 文字を正しくサポートしていない可能性があることです (これだけの問題ではありません)。

于 2012-08-04T18:32:16.277 に答える
0

Windows でデフォルトのコンソール設定 (つまり、ラスター フォント) を使用して実行し、Eclipse 内ではなくコンソールから Java プログラムを実行していると思います。

その場合は、TrueType フォント (Lucida Console または Consolas) を使用するようにコンソール設定を変更するだけで、疑問符の代わりにボックスが表示されるはずです。それらも正しく見えませんが、少なくとも疑問符ではなく実際のテキストです。

補足: 何かがUnicode をサポートしているが、Latin 1 などの別のエンコーディングに変換している場合、疑問符がよく発生します。

于 2012-08-04T18:34:27.480 に答える