Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now