Encoding Text on windows Phone

Hi,
I’m using text retrieved from my sqlite database in my app (utf-8 encoded) the text appears in the ui interface. All working fine on ios and computers but I can’t get it to show correctly on Windows Phone. I assume that is using another encoding.
Does someone know what is default encoding on Windows Phone 8.1 and how to convert it correctly (using scripts)?

Default encoding for what? Generally Windows use UTF16 natively. What does it appear like in UI?

Well, é appears as ©Ã
É as É
À as À
so it seems the problem is being caused by UTF-8 bytes being interpreted as Windows-1252 (or ISO 8859-1) bytes

How are you converting UTF8 to strings? And are you sure that data in your database is indeed UTF8?

Yes my data is UTF8
I use the sqlite plugin here GitHub - robertohuertasm/SQLite4Unity3d: SQLite made easy for Unity3d
then i use the method ToString() on the data.row object.
If I type accents directly on a string there is no problem.
I assume my problem come from the sqlite.dll or the ToString method on windows phone.

What is “data.row”? What type is it?

data.row is System.String

What’s the point of calling ToString on it then? I assume it’s already corrupted at that point? At what point does the System.String get created?

If I don’t use it I have this error
Cannot implicitly convert type object' to string’. An explicit conversion exists (are you missing a cast?). So I guess it is still an object.
What I do is a request on a sqlite database. The result is a Datatable
I use the Datatable.Rows[index][“nameofthecolumn”] to find my storeddata. This data which is a String in my database appears as an object so I have to cast it as a String.
It works fine on all platforms except on Windows phone. It appears the problem is recurrent with the Windows phone internally working in UTF16 like you said.
But how can convert my UTF8 string to UTF16 ?

All System.Strings are already UTF16 - you can’t have an UTF8 System.String. The problem happens somewhere before converting it to a string. UTF8 data is usually represented as “byte[ ]” in C#.

What type is “Datatable.Rows[index]” that you use your indexer on? Does the indexer actually read from the database, or does it merely address it?

Datatable.Rows[index] is of type DataRow

Datatable.Rows[index][“nameofthecolumn”] is of type System.String (so why does it show error ‘Cannot implicitly convert type object' to string’. An explicit conversion exists (are you missing a cast?’ if I try to assign it to a UnityEngine.UI.Text ().text value without applying the ToString() method)

Debug.Log(data.Rows [0]["nameofthecolumn"].GetType().ToString());=>System.String

GameObject.FindWithTag ("randomtag").GetComponent<UnityEngine.UI.Text> ().text = data.Rows [0] ["nameofthecolumn"]; => error CS0266: Cannot implicitly convert type object' to string’. An explicit conversion exists (are you missing a cast?)

Debug.Log(data.Rows [0]["nameofthecolumn"].ToString().GetType().ToString());=>System.String

GameObject.FindWithTag ("randomtag").GetComponent<UnityEngine.UI.Text> ().text = data.Rows [0] ["nameofthecolumn"].ToString (); => All is fine but wrong accents

GetType() gets the type of the object, not the type of a variable. A variable of type Object can point to an object of type String, but it won’t be assignable to a string without a cast.

Anyway, I can’t find type called “DataRow” in the github repository you linked. Did you link the right plugin? Generally, I’d find the place in code where it converts from raw bytes to strings, and check how it does that. Most likely, it’s doing it incorrectly.

Oops. wasn’t pointing at the right plugin.
https://github.com/Busta117/SQLiteUnityKit
Sorry for that

Well here’s your issue:

It uses ANSI encoding to convert text to UTF16, rather than converting using UTF8 to UTF16 converter. And the default ANSI encoding on your phone happens to be Windows-1252.

1 Like

Great thank you !
I changed row [i] = Marshal.PtrToStringAnsi (text); to row [i] = Marshal.PtrToStringUni (text); but I had wrong characters (Utf8 interpreted as UTF16 I suppose).

So I changed the dll script used
[DllImport("sqlite3", EntryPoint = "sqlite3_column_text")] to [DllImport("sqlite3", EntryPoint = "sqlite3_column_text16")] Problem solved ! Nice French accents
Thank you a lot for your help !