Unable to print russian characters

纵然是瞬间 提交于 2019-12-28 06:32:08

问题


I have a russian string which i have encoded to UTF-8

String str = "\u041E\u041A";
System.out.println("String str : " + str);

When i print the string in eclipse console i get ?? Can anyone suggest how to print the russian strings to console or what i am doing wrong here?

I have tried converting it to bytes using byte myArr[] = str.getBytes("UTF-8") and then new String(myArr, "UTF-8") still same problem :-(


回答1:


Try this:

String myString = "some cyrillic text";
byte bytes[] = myString.getBytes("ISO-8859-1"); 
String value = new String(bytes, "UTF-8"); 

Or this:

String myString = "some cyrillic text";
byte bytes[] = myString.getBytes("UTF-8"); 
String value = new String(bytes, "UTF-8"); 

The main problem with russian its to set UTF-8 encoding correctly.




回答2:


In eclipse Go to Run > Run Configuration > Common > Change the console encoding to UTF-8. You will be able to see the Russian Characters in console




回答3:


The display font of the console will most likely not cope with nonASCII characters.

You could try printing to a file rather then System.out




回答4:


My Eclipse prints it correctly

String str : ОК

try to change Run Configurations encoding to UTF-8 or CP1251




回答5:


It's an old topic, but nevertheless maybe below would help someone.

If you're reading data using InputStream / InputStreamReader (for example from some API) that contains Cyrillic symbols and you get some gibberish like ������ ��� or ?????? ???, try to apply encoding Charset as second parameter of InputStreamReader constructor.

EXAMPLE:

Let's use Russian Central Bank API to get US dollars and euro price in Russian rubles. In below code we get data for the current day when we make a request. Data from API is in xml so we also need to parse it.

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

public class CBRFApi {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String output = getAndReadData("http://www.cbr.ru/scripts/XML_daily.asp");
        Document document = loadXMLFromString(output);

        // getting root element
        Node root = document.getDocumentElement();
        NodeList currencies = root.getChildNodes();

        // just for further reference
        Node usDollar;
        Node euro;

        for (int i = 0; i < currencies.getLength(); i++) {
            Node currency = currencies.item(i);
            String key = currency.getAttributes().getNamedItem("ID").getNodeValue();

            if (key.equals("R01235") || key.equals("R01239")) {
                if (key.equals("R01235")) // US dollar ID
                    usDollar = currency;
                else if (key.equals("R01239")) // Euro ID
                    euro = currency;

                NodeList currencySpecs = currency.getChildNodes();
                System.out.print(currencySpecs.item(1).getTextContent());
                System.out.print(" " + currencySpecs.item(3).getTextContent());
                System.out.print(" " + currencySpecs.item(4).getTextContent());
                System.out.println();
            }
      }
    }

    public static String getAndReadData(String link) {
        String output = "";
        try {

            URL url = new URL(link);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/xml");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.2; en-us; SAMSUNG GT-I9505 Build/JDQ39) " +
                    "AppleWebKit/535.19 (KHTML, like Gecko) Version/1.0 Chrome/18.0.1025.308 Mobile Safari/535.19.");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            // below is the key line,
            // without second parameter - Charset.forName("CP1251") - 
            // data in Cyrillic will be returned like ������ ���
            InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream(), Charset.forName("CP1251"));

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                output += line;
            }

            conn.disconnect();

            return output;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Document loadXMLFromString(String xml)
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
            InputSource inputSource = new InputSource(new StringReader(xml));
            return builder.parse(inputSource);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

So proper output is:

USD Доллар США 63,3791
EUR Евро 70,5980

And without indicating Charset.forName("CP1251"):

USD ������ ��� 63,3791
EUR ���� 70,5980

Of course actual encoding in your case may differ from CP1251, so if this one doesn't work, try other encoding.




回答6:


In XCode you can print values by LLDB function po [SomeClass returnAnObject] For automatic work it can be added in some action in a breakpoint. Also you need add checkbox in "Automatic continue after eval actions"



来源:https://stackoverflow.com/questions/17168868/unable-to-print-russian-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!