In a previous post I described some steps on how to enable you NAV environment to support DBCS. Read more about it here: https://kennyvaes.wordpress.com/2009/06/08/nav-vs-dbcs/
Now, the next challange we faced was inserting Japanese characters from an external system into the NAV database.
In other countries we mangage this by running a webservice which insert the information into the SQL database. This webservice is built in C#.
The problem now on the Japanese system was that the strings passed on to the webservice are all Unicode, because .NET handles all strings as Unicode.
Just inserting these Unicode bytes in NAV without conversion will result in a string like “日本 アンユージュアル エスケープ” to be inserted as “???? ??? ????? ???”.
What Navision does is convert this string to a series of Non-Unicode bytes like “ƒAƒ“ƒ†[ƒWƒ…ƒAƒ‹@ƒGƒXƒP[ƒv”
After some time figuring out how to insert these Unicode strings into NAV we came up with the following.
Step 1: Determine the codepages
First of all you need to determine the codepages the server and NAV use. In my case the server was running codepage 1252 Latin encoding. And NAV, because of the Non-Unicode settings, was running under codepage Japanese Shift-JIS (932).
Step 2: Convert your strings
So now you know what codepages you use, the solution is actually pretty straight-forward. What does NAV do? Well, when you input this Japanese string 日本 アンユージュアル エスケープ in NAV. Navision is running codepage 932 and gets the Bytes for this string in 932 encoding. The OS is running codepage 1252 so the SQL server also uses this codepage. What you need to do is convert these 932 bytes into a 1252 encoded string.
The following C# code will do this for you. Remeber to replace the 932 and 1252 encodings with your encoding.
string JapString = “日本 アンユージュアル エスケープ”;
string NavString = “”;
Encoding nav = Encoding.GetEncoding(1252);
Encoding unicode = Encoding.GetEncoding(932);
byte unicodeBytes = unicode.GetBytes(JapString);
NavString = nav.GetString(unicodeBytes);
now the NavString variable contains “ƒAƒ“ƒ†[ƒWƒ…ƒAƒ‹@ƒGƒXƒP[ƒv”.