converting Arabic to hex

728 views
Skip to first unread message

Kiran Raj

unread,
Aug 20, 2009, 1:39:31 AM8/20/09
to DotNetDe...@googlegroups.com
Hi everyone

I am working on text messaging program for Arabic users . i receive the messages in Unicode and store it in the database. A .net windows service will read the message table in the database then it sends the message to a java console. The problem is, the java console only accepts hex. So I need to convert Unicode to hex and convert back to Unicode. how can i do it


i tried the following code and it converts UTF16 to hex but i dont know how to convert it back

    public string StrToHex(string input)
    {
        StringBuilder s = new StringBuilder();
        foreach (char chr in input)
        {
            s.Append(Convert.ToString(chr, 16).PadLeft(4, '0'));
        }
        return s.ToString();
    }

eg مرحبا would be 0645 0631 062D 0628 0627

but 0645 0631 062D 0628 0627 to مرحبا  is the problem

any help on how to accomplish this ?

regards
kiran

Cerebrus

unread,
Aug 20, 2009, 6:31:29 AM8/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The following code seems to work for me. Test it and let me know. I'm
not sure why you are padding the hex string.

---
static void Main(string[] args)
{
string EngToHex = StrToHex("Cerebrus");
string ArabToHex = StrToHex("مرحبا");

string HexToEng = HexToStr("43 65 72 65 62 72 75 73");
string HexToArab = HexToStr("645 631 62D 628 627");
}

private static string StrToHex(string input)
{
StringBuilder s = new StringBuilder();
foreach (char chr in input)
{
int val = Convert.ToInt32(chr);
s.AppendFormat("{0:X} ", val);
}
return s.ToString().Trim();
}

private static string HexToStr(string input)
{
StringBuilder s = new StringBuilder();
string[] hexVals = input.Split(' ');
foreach (string hexVal in hexVals)
{
int val = Convert.ToInt32(hexVal, 16);
char charVal = (char)val;

s.Append(charVal);
}
return s.ToString();
}
---

Processor Devil

unread,
Aug 20, 2009, 3:13:34 AM8/20/09
to DotNetDe...@googlegroups.com
These two functions are what you seek
            byte[] System.Text.UnicodeEncoding.Unicode.GetBytes(string s);
            string System.Text.UnicodeEncoding.Unicode.GetString(byte[] bytes);


2009/8/20 Kiran Raj <23k...@gmail.com>

Kiran Raj

unread,
Aug 20, 2009, 7:09:19 AM8/20/09
to DotNetDe...@googlegroups.com
thanks devil and Cerebrus

both methods are converting arabic to hex. but can i display the text in a console window. i tried the following code, console displays the string as "??????" .  does arabic text dispaly in browsers alone?


       static void Main(string[] args)
        {
            string EngToHex = StrToHex("Cerebrus");
            string ArabToHex = StrToHex("مرحبا");

            string HexToEng = HexToStr("43 65 72 65 62 72 75 73");
            string HexToArab = HexToStr("645 631 62D 628 627");

            Console.WriteLine(ArabToHex);

            Console.WriteLine(HexToArab);

        }

2009/8/20 Cerebrus <zor...@sify.com>

Cerebrus

unread,
Aug 20, 2009, 8:30:58 AM8/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The text displays appropriately if you use a Text visualizer in Debug
mode. However, when writing to the standard console output, it does
display as "?" characters. Possibly, these characters are not
supported in the standard console outputs.

Calling code should receive correct data, in any case.

Kiran Raj

unread,
Aug 20, 2009, 9:08:07 AM8/20/09
to DotNetDe...@googlegroups.com
ok thank you.
Reply all
Reply to author
Forward
0 new messages