Solved

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

Posted on 2010-09-20
5
665 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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