简体   繁体   English

Java中的字符编码不起作用

[英]Character encoding in Java not working

I am currently working on a program that basically translates between English and Russian. 我目前正在研究一种基本上在英语和俄语之间翻译的程序。 However, when entering a Russian word, some of the characters are not encoded properly. 但是,输入俄语单词时,某些字符编码不正确。

For example, 'привет', works fine but for 'яблоко', I get: ' ?блоко' after encoding. 例如,'привет'可以正常工作,但是对于'яблоко',我在编码后得到:' ?блоко'。

The code I have for the encoding is: 我用于编码的代码是:

String s = new String(searchText.getBytes(), "UTF-8");
System.out.println(s);

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks. 谢谢。

"UTF-8" is specifying that the String you are receiving is in UTF-8 format. “ UTF-8”指定您接收的字符串为UTF-8格式。 Most likely it is in a different format. 它很可能采用其他格式。

If you use the correct encoding it should be fine. 如果使用正确的编码,应该没问题。

You need to make sure you have the necessary encoding setup on your console. 您需要确保控制台上具有必要的编码设置。 If you are on windows command prompt, execute first- 如果您在Windows命令提示符下,请先执行-

chcp 65001 && start.bat chcp 65001 && start.bat

If you are using Eclipse, do- 如果您使用的是Eclipse,请执行以下操作:

Run Configuration -> Common-> Encoding -> Other -> UTF-8 运行配置->通用->编码->其他-> UTF-8

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

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