Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Conversion of  Arabic to Database Compatiable

Posted on 2011-03-22
7
Medium Priority
?
598 Views
Last Modified: 2012-05-11
We have to develop a C#.Net (VS 2008) Windows application on top of an exisitng legacy SQL2000 database. These database contain some tables with Arabic data stored with data type varchar.
While reviewing the same, we foudn that the arabic data is stored in some format which we are not aware of.  However we found a way to display the same data into C# edit box using the following Function

 private string ToArabic(string text)
    {
      byte[] bytes = new byte[text.Length];
      for (int i = 0; i < text.Length; i++)
        bytes[i] = (byte)text[i];
      
      return System.Text.Encoding.GetEncoding(1256).GetString(bytes);
    }

Open in new window


This will convert  ÓíãÇÌí       to ¿¿¿¿¿¿

We are now in search of a reverse function. i.e Arabic to Database Format.
If user enters ¿¿¿¿¿¿ , it should convert to ÓíãÇÌí

We need a way to store any arabic data eneterd  by user in a C# edit box to the database in the same format in which it currently holds arabic data. We are forbidden from making any changes to the way in which data is stored in database

Examples of data stored in database on left and how it gets converted to Arabic(on right) using the function above is shown below
ÓíãÇÌí      ¿¿¿¿¿¿
Ñãíæ      ¿¿¿¿
ÚÈÏÇáÓáÇã      ¿¿¿¿¿¿¿¿¿

We need a way to convert the same back . This is urgent and hence 500 points. Please help
0
Comment
Question by:GEMINI-INDIA
  • 4
  • 2
7 Comments
 

Author Comment

by:GEMINI-INDIA
ID: 35188742
Please refer the attached image as in the conversion shown in the question, the ARabic has not come out correctly(editor limiation) Conversion of Data to Arabic
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 35188769
>> We are now in search of a reverse function. i.e Arabic to Database Format.

I need to clarify you better..
Arabic characters are considered as unicode characters and hence it has to be stored in either nchar() or nvarchar().
Seems like you have saved Arabic characters into a table with column datatype either char() or varchar() which doesn't have the ability to store unicode values and hence when you inserted Arabic data is inserted into your table, you have lost the Arabic data ( and hence ¿¿¿¿¿¿ is not Database format)

In order to store Arabic data, you need to store it in column with datatype either nchar() or nvarchar() only.
While inserting Arabic data into the table, Unicode(arabic) characters needs to be prefixed with N like shown below

INSERT into ur_table ( arabic_data_column)
VALUES (N'abc');

assuming 'abc' is Arabic data..

Hope this clarifies.
0
 

Author Comment

by:GEMINI-INDIA
ID: 35188886
Hi,
Thanks for the reply. However there seems to be some confusion on the requirement. Please refer to our question again. We have no control on the database and how it stores data.(It is accessed by other applications also).  Please also note that  ¿¿¿¿¿¿  is not the issue as it was only an editor issue in Experts exhange.  Please refer to the addendum image comment to our first question in which the conversion of characters like ÓíãÇÌí      has been successfuly done by the function mentioned. We are looking for a reverse function which will convert arabic to the formats like ÓíãÇÌí      

Hope this clarifies
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 35189110
>> We are now in search of a reverse function. i.e Arabic to Database Format.

Ok, got your situation clear now..
Since it is the logic already used by the application, instead of re-inventing the wheel you can use the logic done over there itself right
Or else, just use the appropriate decoding for the encoding done in the code posted above.
0
 

Author Comment

by:GEMINI-INDIA
ID: 35189168
Hi,
Thanks for the reply. However we are creating a new application and have no idea how the exisitng application(by other providers) managed to save Arabic data in format like  ÓíãÇÌí
into the database

However we have found a mechanism to convert ÓíãÇÌí  to arabic using function below

private string ToArabic(string text)
    {
      byte[] bytes = new byte[text.Length];
      for (int i = 0; i < text.Length; i++)
        bytes[i] = (byte)text[i];
      
      return System.Text.Encoding.GetEncoding(1256).GetString(bytes);
    }

Open in new window


We now need a reverse function to convert Arabic from our new application to format like ÓíãÇÌí  so that our data saving to the database does not affect other applications
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35190632
I think it should be:

private string ToArabicReverse(string text)
    {
      byte[] bytes = System.Text.Encoding.GetEncoding(1256).GetBytes(text);
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < bytes.Length; i++)
        sb.Append((char)bytes[i]);
      
      return sb.ToString();
    }

Open in new window


BTW, 1256 is called Arabic (Windows) encoding.

I hope this helps.
0
 

Author Comment

by:GEMINI-INDIA
ID: 35195928
Thank you wdosanjos. Your solution works. Points awarded

Thanks again
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question