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 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])+",";
}
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?!
Why do i want to convert a word to UTF-8 or UTF-16 ?
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 .
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.
Is my original code reliable?
Yes it is. If I use the English characters it responses very well.
What are my original codes ?
Java codes which send parameter to the server :
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# codes which response to the clients :
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 ?**