本文介绍了在Java(JSP)中将十进制NCR代码转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将FARSI中的字符串解码为UTF-8,但随后我检查了浏览器本身将FARSI字符串转换为十进制NCR代码

I was trying to decode string which in FARSI to UTF-8 but then i checked browser itself convert FARSI string into Decimal NCRs Code

我如何将十进制NCR代码转换为UTF-8?

How i can convert Decimal NCRs Code into UTF-8 ??

String farMsg = "عتباري";
String finalMsg = new String(farMsg.getBytes(),"UTF-8");
System.out.println("\n Farsi Message \n" + finalMsg);

当我尝试使用上述代码进行转换时,它工作正常但是,如果我在JSP页面的输入框中使用相同的字符串,则会给我这样的输出

when i am trying convert using above code it is working finebut if i am using same string from an input box of JSP page its is giving me some output like this

عتباري

要进行相同的修改,我需要做些什么修改.

What modification do I need to do for converting into same.

编辑

推荐答案

我创建了一个自定义函数,该函数将DecimalNCR转换为String.

I created a custom function which converts DecimalNCR to String.

public static String ConvertDecimalNCRToString(String hex)
{
    String myString = hex.replace("&#", "");
    String[] split = myString.split(";");
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < split.length; i++)
    {
        sb.append((char)Integer.parseInt(split[i]));
    }
    return sb.toString();
}

这将成功转换您提供的String.

This converts successfully your supplied String.

编辑我用中文游鍚堃你好你怎么样和波斯语(عتباريمرحبا كيف حالك)字符测试了上面的功能,它提供了正确的结果.

EDIT I tested the above function with Chinese 游鍚堃,你好你怎么样 and Farsi (عتباري , and مرحبا كيف حالك) character it provided correct results.

这篇关于在Java(JSP)中将十进制NCR代码转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:16