将字符串从Windows字符集转换为Java中的UTF 8 [英] converting String from Windows charset to UTF 8 in Java

查看:66
本文介绍了将字符串从Windows字符集转换为Java中的UTF 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须给从.bat文件调用的Java应用程序提供一些参数。这样做会使参数具有系统的字符集编码,从而使某些字符显示错误。我尝试过

so I have to give some arguments to my Java app which is called from a .bat file. Doing this makes the arguments have the system's charset encoding, which makes some characters displayed wrongly. I tried this

     String titulo;

     titulo = new String (args[1].getBytes(),"Cp1252");

还尝试了与此列表中的其他一些 http://docs.oracle.com/javase/1.4.2/docs/guide/intl/encoding .doc.html ,但没有一个成功。
如何将Windows字符集的字符串编码为Java的UTF 8?
提前谢谢!

also tried with a few others from this list http://docs.oracle.com/javase/1.4.2/docs/guide/intl/encoding.doc.html and none of them succeeded. How else can I encode a String from Windows charset to Java's UTF 8? Thanks a lot in advance!

罗德里戈(Rodrigo)。

Regards, Rodrigo.

编辑:我在.bat中给出的参数是Martín,输出(显示为JLabel)显示了该MartÝn。

The argument I give in the .bat is Martín and the output (which is a JLabel displaying) shows this MartÝn.

推荐答案

Windows命令提示符 cmd.exe 实际上不使用CP1252。它使用什么显然取决于系统。在西欧系统中,很可能是 CP850 。因此,您可以尝试以下操作:

The Windows command prompt cmd.exe actually doesn't use CP1252. What it uses apparently depends on the system; on Western European systems it's most likely CP850. So you can try this:

titulo = new String (args[1].getBytes(),"Cp850");

您可以查看用于cp850的代码表,以检查应该发生的情况:í是拉丁语1中的字节ED(扩展名为cp1252),而cp850中的字节ED是Ý。因此:如果从Java GUI将í打印到 cmd.exe ,它将显示为Ý。

You can look at the code tables for cp850 to check what should happen: í is the byte ED in latin 1 (and, by extension, cp1252), and the byte ED in cp850 is Ý. Therefore: if you print "í" from a Java GUI to cmd.exe it will show up as "Ý".

(但是您似乎看到了相反的情况:终端上的í在GUI中显示为Ý。. cmd.exe 应该将字节A1传递给Java,Java应该将其解释为¡。)

(But you seem to be seeing the reverse: "í" from the terminal shows up as "Ý" in a GUI.. that doesn't make sense, cmd.exe should pass the byte A1 to Java, which should interprete it as "¡"..)

这篇关于将字符串从Windows字符集转换为Java中的UTF 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆