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

Posted on 2010-09-20
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"
Question by:jerra
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
LVL 12

Expert Comment

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++) .{


LVL 12

Expert Comment

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);

Author Comment

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).
LVL 12

Accepted Solution

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) {
                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


Author Closing Comment

ID: 33722456

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS question 16 63
Send parameters via datatable ajax call 4 48
AJAX Create / Update Not working MVC 5 67
C#: How do I test for null objects and a dub a value? 2 37
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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