Java 作为环境变量传递时无法解码货币符号的unicode
以下代码适用于卢比符号Java 作为环境变量传递时无法解码货币符号的unicode,java,unicode,Java,Unicode,以下代码适用于卢比符号 String encode ="\u20B9"; byte[] ptext = encode.getBytes(StandardCharsets.UTF_8); String value = new String(ptext,StandardCharsets.UTF_8); System.out.print(value); It prints : ₹ 但当我们将卢比符号unicode作为环境变量传递时,它就不起作用了 String encode = System.ge
String encode ="\u20B9";
byte[] ptext = encode.getBytes(StandardCharsets.UTF_8);
String value = new String(ptext,StandardCharsets.UTF_8);
System.out.print(value);
It prints : ₹
但当我们将卢比符号unicode作为环境变量传递时,它就不起作用了
String encode = System.getenv("encode");
byte[] ptext = encode.getBytes(StandardCharsets.UTF_8);
String value = new String(ptext,StandardCharsets.UTF_8);
System.out.print(value);
It prints : \u20B9
这是我在mac OS上尝试过的。Java只理解Java源文件中的
\uxxx
格式,编译器处理它(解析它并将其转换为单个字符)。如果从某处读取该值,它将不会被解释为unicode字符(在.properties文件中除外,这有点特殊)。Copy Paste₹代码>字符
粘贴实际的₹代码>字符,而不是使用Kayaman在中指出的\u
转义
在2018年2月2日内与(由)合作
《世界报》你好
10.0.1
午餐
什么是主机操作系统?将详细信息作为编辑添加到您的问题中,而不是作为注释。当您在不经过无用的解码/编码步骤的情况下打印值时会发生什么情况?String encode=System.getenv(“encode”);它打印:\u20B9这意味着环境变量的值实际上是“\\u20B9”
(其中“\\\”
是内存中的单个“\\”
)。您必须去掉前面的“\\u”
,使用Integer.parseInt(“20B9”,16)
,将int
转换成字符,然后将字符转换成字符串。
package com.basilbourque.example;
public class Env {
// Example passing Unicode characters via system environment variable.
public static void main ( String[] args ) {
System.out.println( "Bonjour tout le monde." );
System.out.println( System.getProperty( "java.version" ) );
System.out.println( System.getenv( "wazzup" ) );
System.out.println( System.getenv( "fire" ) );
System.out.println( System.getenv( "rupee" ) );
}
}