简体   繁体   English

从Java中的注册表读取Unicode字符

[英]Read unicode characters from registry in java

In my Application i am reading registry to get all TimeZone names. 在我的应用程序中,我正在阅读注册表以获取所有TimeZone名称。 It is working fine with native english OS machine. 它可以与本机英语OS机器一起正常工作。

But for chinese native os it is showing "????????". 但是对于中国本地操作系统,它显示“ ????????”。

I am using WinRegistry.java, the common file available to read registry in java. 我正在使用WinRegistry.java,这是可用于读取Java中注册表的通用文件。

Below is the method that reads bytes from registry, but that bytes contains junk characters only. 下面是从注册表读取字节的方法,但是该字节仅包含垃圾字符。

private static String readString(Preferences root, int hkey, String key, String value)
    throws IllegalArgumentException, IllegalAccessException,
    InvocationTargetException 
  {
    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
    if (handles[1] != REG_SUCCESS) {
      return null; 
    }
    byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
        new Integer(handles[0]), toCstr(value) });
    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
    return (valb != null ? new String(valb).trim() : null);
  }

In valb[] byte array i am getting the junk characters, so whatever encoding i use to convert that byte array to string, i am getting junk characters only. 在valb []字节数组中,我得到了垃圾字符,因此,无论使用哪种编码将字节数组转换为字符串,我都只会得到垃圾字符。 Can any one suggest me, what changes in this method will make it work fine?? 谁能建议我,这种方法的哪些变化将使其正常工作?

I assume you mean the hack in this answer that uses a private, undocumented, implementation-specific API. 我认为您的意思是此答案的hack,它使用私有的,未记录的,特定于实现的API。

This code uses the default encoding to turn the returned bytes into chars: 此代码使用默认编码将返回的字节转换为char:

return (valb != null ? new String(valb).trim() : null);

On Windows, the default encoding will likely be a legacy encoding - an "ANSI" code page . 在Windows上,默认编码可能是旧式编码- “ ANSI”代码页

You will need to figure out the encoding of the data and provide it explicitly in the String(byte[],Charset) constructor or switch to a documented API - eg using RegQueryValueExW with JNA . 您将需要弄清楚数据的编码,并在String(byte[],Charset)构造函数中显式提供数据,或切换到有文档的API-例如,将RegQueryValueExWJNA结合使用

As Octopus pointed out in the comments, it is also easy to turn characters into junk by using System.out as it uses lossy legacy encodings too. 正如Octopus在评论中指出的那样,使用System.out也很容易将字符变成垃圾,因为它也使用有损的旧式编码。

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

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