Inserting Japanese characters into a NAV database from outside NAV


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”.

Great!!

Advertisements
Tagged with: , ,
Posted in .NET, NAV
3 comments on “Inserting Japanese characters into a NAV database from outside NAV
  1. edison says:

    hi kenny vaes,
    “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”.”

    sorry how to i activate this code, and what would i save this file or modify into?

  2. Kenny Vaes says:

    This code was intended to be placed inside a webservice that connects to the NAV SQL Database.
    The result of this method can be placed into a SQL column of a NAV table. The NAV client will then encode it to the correct charset.

  3. Kaoru says:

    Hi Kenny,
    I have a question regarding the Japanese double byte character. As you know, in Navision report, system will do line break if there is ‘/’ sign in the sentence. However, there are some double byte characters that has ‘/’ or ‘¥’ that make up part of the word. is there any way to avoid the sign to take effect so the word can display completely?
    Thanks!
    Kaoru

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: