Solved

C#, problem with Unicode conversion. (.NET 4)

Posted on 2010-09-20
5
670 Views
Last Modified: 2013-12-16
I just can't get this working.

I have a string read from a source, for example "0038003800380038" which is Unicode. Thing is that the Unicode escape character is missing. I want to convert it to ASCII (8888). How do I do it?

"0038003800380038" -> "\u0038\u0038\u0038\u0038" -> "8888"
0
Comment
Question by:jerra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 33715192
The problem with unicode is that you cannot be sure that there are always 4 digits per character.

string oldString = "003800380038";
string newString = "";

for (int i=0; i < oldString.Length;i++) .{

}

0
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 33715231
Sorry.. I hit the wrong keys so it was sent too early. I would try something like this:

            string oldString = "003800380038";
            string newString = "";

            while (oldString.Length >= 4) {
                newString += ("\\u" + oldString.Substring(0,4));
                oldString = oldString.Substring(4);
            }
0
 

Author Comment

by:jerra
ID: 33715266
Sorry I have been too vague.
The problem is not adding the escape char. The problem is getting the string converted to "8888". If I would output newString it would just show "\u0038".
All Unicode (utf16) characters are represented by hexadecimal numbers from 0000 to FFFF (in my case).
0
 
LVL 12

Accepted Solution

by:
HugoHiasl earned 500 total points
ID: 33716732
Hmm ok... Should be done by this:


string oldString = "003800380038";
            List<byte> byteList = new List<byte>();

            while (oldString.Length >= 4) {
                byteList.Add(Byte.Parse(oldString.Substring(2,2),System.Globalization.NumberStyles.HexNumber));
                byteList.Add(Byte.Parse(oldString.Substring(0,2),System.Globalization.NumberStyles.HexNumber));
                oldString = oldString.Substring(4);
            }

            Byte[] bytes = byteList.ToArray<Byte>();
            Encoding enc = Encoding.GetEncoding("utf-16");
            string output = enc.GetString(bytes, 0, bytes.Length);

Open in new window

0
 

Author Closing Comment

by:jerra
ID: 33722456
Thanks!
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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