如何使 unicode 从方法 return 中输入 java 打印
How do make unicode to print from method return type in java
我正在使用下面的语句在 java
中将 unicode 打印为字符串
System.out.println("\u0917\u094d\u0930\u093e\u0939\u0915");
它在控制台上打印ग्राहक。
现在我不是在 println() 中传递简单的字符串,而是像这样使用
System.out.println(this.HindiConvertUni("ग्राहक"));
public String HindiConvertUni(String str)
{
StringBuffer ostr = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be converted to unicode?
{
ostr.append(ch); // No.
} else // Yes.
{
ostr.append("\u"); // standard unicode format.
// Get hex value of the char.
String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);
// Prepend zeros because unicode requires 4 digits
for (int j = 0; j < 4 - hex.length(); j++)
{
ostr.append("0");
}
// standard unicode format.
ostr.append(hex.toLowerCase());
}
}
return (new String(ostr));
}
现在我想打印我用作参数的字符串。但它打印成 \u0917\u094d\u0930\u093e\u0939\u0915
我希望将其打印为 ग्राहक 而不是 unicode。
如何使它更高级possible.Thanx。
实际上我正在使用 iText 来制作我正在做的报告
BaseFont unicode = BaseFont.createFont("/home/mani/current/ARIALUNI.TTF",
BaseFont.IDENTITY_H, true);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
PdfPCell customerLblCell = new PdfPCell(new Phrase("\u0917\u094d\u0930\u093e\u0939\u0915",
font));
但是我看起来类似于
PdfPCell customerLblCell = new PdfPCell(new Phrase(this.HindiConvertUni("ग्राहक"),
font));
但它打印的是 unicode 而不是 ग्राहक!
我正在尝试像上图那样打印 ऐसा करने की कोशिश 但它确实像上图一样。
我相信您对 Java 代码中的转义 unicode 字符存在误解。
String s = "ग्राहक";
String t = "\u0917\u094d\u0930\u093e\u0939\u0915";
System.out.println("s = " + s);
System.out.println("t = " + t);
System.out.println("s equals t: " + s.equals(t));
输出
s = ग्राहक
t = ग्राहक
s equals t: true
如您所见,两个字符串相等。所以 new Phrase("ग्राहक", font)
和 new Phrase("\u0917\u094d\u0930\u093e\u0939\u0915", font)
产生相同的代码。要么两者都在工作,要么都没有工作。
原因是像 "\u0917"
这样的转义 unicode 字符将在 编译时 由编译器替换为 unicode 字符本身 "ग्"
.
另一方面,您的方法 HindiConvertUni("ग्राहक")
在 运行时 生成一个字符串 "\u0917\u094d\u0930\u093e\u0939\u0915"
,它不会自动转换为相应的 unicode 字符。
修改以上代码段
String x = HindiConvertUni("ग्राहक");
System.out.println("x = " + x);
System.out.println("x equals t: " + x.equals(t));
输出是
s = ग्राहक
t = ग्राहक
s equals t: true
x = \u0917\u094d\u0930\u093e\u0939\u0915
x equals t: false
也许这使区别变得明显。
在 OP 评论后添加
据我所知,在搜索互联网时,问题似乎与以下事实有关(一切AFAIK 因为我不 read/speak 印地语)。印地语是从左到右书写的,但这里有例外,例如您使用元音 'I' (unicode \u093F) 的情况。在字符编码中,它位于辅音字母的右侧,然后 'I' 呈现在该辅音字母的左侧。两个字符都呈现在相同的垂直位置。
用于演示。
System.out.println("current : \u0936 - \u0936\u093F");
System.out.println("expected : \u0936\u093F - \u0936");
System.out.println("separated: \u0936 - \u093F - \u0936");
输出
current : श - शि
expected : शि - श
separated: श - ि - श
根据一些想法,您的情况可能是什么原因。
- unicode 字符的顺序是错误的,您实际上是从数据库中以“\u0936\u0936\u093F”而不是“\u0936\u093F\u0936”
获取它们
- 您没有使用最新的
iText
版本(参见 comment and linked ones from Bruo Lowagie)
- 或者 iText 可能无法处理这种特定情况,无法基于此 test code (if this should be the case, have a look in the source 完全证明它,修复它并发送请求请求)
System.out.println("\u0917\u094d\u0930\u093e\u0939\u0915");
它在控制台上打印ग्राहक。 现在我不是在 println() 中传递简单的字符串,而是像这样使用
System.out.println(this.HindiConvertUni("ग्राहक"));
public String HindiConvertUni(String str)
{
StringBuffer ostr = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if ((ch >= 0x0020) && (ch <= 0x007e)) // Does the char need to be converted to unicode?
{
ostr.append(ch); // No.
} else // Yes.
{
ostr.append("\u"); // standard unicode format.
// Get hex value of the char.
String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);
// Prepend zeros because unicode requires 4 digits
for (int j = 0; j < 4 - hex.length(); j++)
{
ostr.append("0");
}
// standard unicode format.
ostr.append(hex.toLowerCase());
}
}
return (new String(ostr));
}
现在我想打印我用作参数的字符串。但它打印成 \u0917\u094d\u0930\u093e\u0939\u0915 我希望将其打印为 ग्राहक 而不是 unicode。 如何使它更高级possible.Thanx。 实际上我正在使用 iText 来制作我正在做的报告
BaseFont unicode = BaseFont.createFont("/home/mani/current/ARIALUNI.TTF",
BaseFont.IDENTITY_H, true);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
PdfPCell customerLblCell = new PdfPCell(new Phrase("\u0917\u094d\u0930\u093e\u0939\u0915",
font));
但是我看起来类似于
PdfPCell customerLblCell = new PdfPCell(new Phrase(this.HindiConvertUni("ग्राहक"),
font));
但它打印的是 unicode 而不是 ग्राहक!
我正在尝试像上图那样打印 ऐसा करने की कोशिश 但它确实像上图一样。
我相信您对 Java 代码中的转义 unicode 字符存在误解。
String s = "ग्राहक";
String t = "\u0917\u094d\u0930\u093e\u0939\u0915";
System.out.println("s = " + s);
System.out.println("t = " + t);
System.out.println("s equals t: " + s.equals(t));
输出
s = ग्राहक
t = ग्राहक
s equals t: true
如您所见,两个字符串相等。所以 new Phrase("ग्राहक", font)
和 new Phrase("\u0917\u094d\u0930\u093e\u0939\u0915", font)
产生相同的代码。要么两者都在工作,要么都没有工作。
原因是像 "\u0917"
这样的转义 unicode 字符将在 编译时 由编译器替换为 unicode 字符本身 "ग्"
.
另一方面,您的方法 HindiConvertUni("ग्राहक")
在 运行时 生成一个字符串 "\u0917\u094d\u0930\u093e\u0939\u0915"
,它不会自动转换为相应的 unicode 字符。
修改以上代码段
String x = HindiConvertUni("ग्राहक");
System.out.println("x = " + x);
System.out.println("x equals t: " + x.equals(t));
输出是
s = ग्राहक
t = ग्राहक
s equals t: true
x = \u0917\u094d\u0930\u093e\u0939\u0915
x equals t: false
也许这使区别变得明显。
在 OP 评论后添加
据我所知,在搜索互联网时,问题似乎与以下事实有关(一切AFAIK 因为我不 read/speak 印地语)。印地语是从左到右书写的,但这里有例外,例如您使用元音 'I' (unicode \u093F) 的情况。在字符编码中,它位于辅音字母的右侧,然后 'I' 呈现在该辅音字母的左侧。两个字符都呈现在相同的垂直位置。
用于演示。
System.out.println("current : \u0936 - \u0936\u093F");
System.out.println("expected : \u0936\u093F - \u0936");
System.out.println("separated: \u0936 - \u093F - \u0936");
输出
current : श - शि
expected : शि - श
separated: श - ि - श
根据一些想法,您的情况可能是什么原因。
- unicode 字符的顺序是错误的,您实际上是从数据库中以“\u0936\u0936\u093F”而不是“\u0936\u093F\u0936” 获取它们
- 您没有使用最新的
iText
版本(参见 comment and linked ones from Bruo Lowagie) - 或者 iText 可能无法处理这种特定情况,无法基于此 test code (if this should be the case, have a look in the source 完全证明它,修复它并发送请求请求)