简体   繁体   English

Java(Linux)和Windows系统之间的字符编码

[英]Character encoding between Java (Linux) and Windows system

I have a simple program that makes a request to a remote server running a service which I believe is written in Delphi, but definately running on Windows. 我有一个简单的程序,它向运行我认为是用Delphi编写但肯定在Windows上运行的服务的远程服务器发出请求。

I'm told the service will be using whatever the default encoding is for Windows. 我被告知该服务将使用Windows的默认编码。

When I get a response and use println to output it I'm getting some strange symbols in the output, which make me think it is a character encoding issue. 当我得到一个响应并使用println进行输出时,我在输出中得到了一些奇怪的符号,这使我认为这是一个字符编码问题。

How can I tell Java the the input from the remote system is in the windows encoding? 我怎样才能告诉Java远程系统的输入是在Windows编码中?

I have tried the following: 我尝试了以下方法:

_receive = new BufferedReader(new InputStreamReader(_socket.getInputStream(),"ISO-8859-1"));
_System.out.println(_receive.readLine());

The extra characters appear as squares in the output with 4 numbers in the square. 多余的字符在输出中显示为正方形,正方形中有4个数字。

Unless you KNOW what the "default encoding" is, you can't tell what it is. 除非您知道“默认编码”是什么,否则您将无法知道它是什么。 The "default encoding" is generally the system-global codepage, which can be different on different systems. “默认编码”通常是系统全局代码页,在不同系统上可能不同。

You should really try to make people use an encoding that both sides agree on; 您应该真正尝试使人们使用双方都同意的编码。 nowadays, this should almost always be UTF-16 or UTF-8. 如今,这几乎应该总是UTF-16或UTF-8。

Btw, if you are sending one character on the Windows box, and you receive multiple "strange symbols" on the Java box, there's a good chance that the Windows box is already sending UTF-8. 顺便说一句,如果您要在Windows框上发送一个字符,并且在Java框上收到多个“奇怪的符号”,则Windows框很有可能已经在发送UTF-8。

使用cp1252代替ISO-8859-1 ,因为Windows上是默认设置。

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

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