解码UTF问题? [英] Decoding UTF Issue?

查看:172
本文介绍了解码UTF问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的Android项目和放大器;我有一个问题异国这让我疯了。我试图将字符串转换为 UTF-16 UTF-8 的。
我用这段代码来实现它,但它给了我一些负面的成员阵列

I am working on my android project & i have an exotic problem which makes me crazy. I am trying to convert a String to Utf-16 or Utf-8. I use this piece of code to achieve it but it gives me an array with some negative members!

的Java代码:<! / p>

Java Code :

String Tag="سیر";
String Value="";
try{
            byte[] bytes = Tag.getBytes("UTF-16");
            for(int i=0;i<bytes.length;i++){
            Value=Value+String.valueOf(bytes[i])+",";
        }



数组成员
数组成员是 [ - 1,-2,51,6,-52,6,49,6] 。我检查了的 UTF-16 的表。它没有任何负数,也是我用一个网站,转换词的 UTF-16M 的。它给了我0633 06CC 0631 HEX 。如果您更改此号码为十进制,你会看到:1577 1740 1585。如您所见,这里没有负数!所以我的第一个问题是什么是这些负数?

Array members : Array members are [-1,-2,51,6,-52,6,49,6]. I checked the UTF-16's table . It doesn't have any negative number and also I used a website which converts words to UTF-16M. It gave me "0633 06CC 0631"HEX. If you change this number to decimal you will see this: "1577 1740 1585". as you see there is no negative number here! So my first question is what are these negative numbers?!

为什么我要一个字转换为UTF-8或UTF-16?

我工作的一个项目。这个项目事呢两部分。第一部分是一个Android应用程序,它发送关键词到服务器。这句话是由客户端发送的。我的客户端使用(波斯语,فارسی)字符。
中的第二部分是由C#&放大器制成的网络应用程序;它具有响应我的客户。

I am working on a project . this project hast two parts. First part is an android application which sends key words to the server. The words are sent by clients. My clients use (persian,فارسی ) characters. the second part is a web application which is made by C# & it has to response to my clients .

问题:当我把这些话给它适用于流服务器????而不是正确的字。我已经尝试了许多办法来解决这个问题,但他们没能解决它。从那以后,我决定派的 UTF-16 UTF-8 的字符串自己到服务器并将其转换为正确的单词。所以我选择,我在我的文章的顶部描述的那些方法。

Problem: When I send these words to the server it works on a stream of "????" instead of the correct word. I have tried many ways to solve this problem but they couldn't solve it. after that i decided to send the utf-16 or utf-8 of string myself to the server and convert it to the correct word. So I chose those method which i described at the top of my post.

是我的原代码可靠吗?

是的。如果我使用英文字符对此的反应非常好。

Yes it is. If I use the English characters it responses very well.

什么是我的原代码?

的Java其中发送代码参数到服务器:

    protected String doInBackground(String...Urls){
                String Data="";
                HttpURLConnection urlConnection = null; 
                try{
                    URL myUrl=new URL("http://10.0.2.2:80/Urgence/SearchResault.aspx?Tag="+Tag);
                    urlConnection = (HttpURLConnection)myUrl.openConnection();      
                    BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));         
                    String temp=""; 
                    // Data is used to store Server's Response 
                    while((temp=in.readLine())!=null)
                    {               
                         Data=Data+temp;        
                    }    
                }

C#代码,响应客户端:

    string Tag = Request.QueryString["Tag"].ToString();
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["conStr"].ToString();
            SqlCommand cmd = new SqlCommand("FetchResaultByTag");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@NewsTag",Tag);
            cmd.Connection = con;
            SqlDataReader DR;
            String Txt = "";
            try
            {
                con.Open();
                DR = cmd.ExecuteReader();
                while (DR.Read())
                {
                    Txt = Txt + DR.GetString(0) + "-" + DR.GetString(1) + "-" + DR.GetString(2) + "-" + DR.GetString(3) + "/";
                }
                //Response.Write(Txt);
                con.Close();
            }
            catch (Exception ex)
            {
                con.Close();
                Response.Write(ex.ToString());
            }

*你觉得呢?你有什么想法?**

*What do you think ? do you have any idea ?**

推荐答案

我解决它。起初,我改变了我的java code.i使用的URLEncoder类的转换我的字符串为UTF-8。

I solved it . at first i changed my java code.i converted my String to UTF-8 by using of URLEncoder class.

新的Java代码:

try{
            Tag=URLEncoder.encode(Tag,"UTF-8");
            }
            catch(Exception ex){
                Log.d("Er>encodeing-Problem",ex.toString());     
            } 



之后我把它作为通过HTTP协议查询字符串

after that i sent it as a query String via Http Protocol

protected String doInBackground(String...Urls){
            String Data="";
            HttpURLConnection urlConnection = null; 
            try{
                URL myUrl=new URL("http://10.0.2.2:80/Urgence/SearchResault.aspx?Tag="+Tag);
                urlConnection = (HttpURLConnection)myUrl.openConnection();      
                BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));         
                String temp=""; 
                // Data is used to store Server's Response 
                while((temp=in.readLine())!=null)
                {               
                     Data=Data+temp;        
                }  

和末我在服务器捕获并解码它。

and at the end i Caught in the server and decoded it .

新的C#代码:

     string Tag = Request.QueryString["Tag"].ToString();
     SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["conStr"].ToString());
            SqlCommand cmd = new SqlCommand("FetchResaultByTag");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@NewsTag",   HttpUtility.UrlDecode(Tag));
cmd.Connection = con;
        SqlDataReader DR;
        String Txt = "";
        try
        {
            con.Open();
            DR = cmd.ExecuteReader();
            while (DR.Read())
            {
                Txt = Txt + DR.GetString(0) + "-" + DR.GetString(1) + "-" + DR.GetString(2) + "-" + DR.GetString(3) + "/";
            }
            Response.Write(Txt);
            con.Close();
        }
        catch (Exception ex)
        {
            con.Close();
            Response.Write(ex.ToString());
        }

这篇关于解码UTF问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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