Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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