0

I found a similar question:

Converting integers to UTF-8 (Korean)

But I can't figure out how you would do this in .net c#

Problem: I have a string from a database - "\354\202\254\354\232\251\354\236\220\354\203\201\354\204\270\354\240\225\353\263\264\354\236\205\353\240\245"

That should translate to - 사용자상세정보입력

Any help would be greatly appreciated!

Community
  • 1
  • 1
kdavej
  • 61
  • 10
  • 2
    We cannot give you an answer without an example of what you've tried. Stackoverflow exists to fix broken code, not write code for you. – Magus Jun 17 '14 at 21:40
  • @Magus: Normally I post code examples but in this case the problem was so far outside of my (albeit limited) knowledge I didn't even know where to begin to code so I posted as I did above. Even that question represents ~4-5 hours worth of research seeking an answer or path to an answer. The steps outlined by Douglas are really what I was looking for - not necessarily the code. Stackoverflow is a valuable resource, one I hope to be knowledgeable enough to contribute back to someday. I apologize for my ignorance of the standards. – kdavej Jun 18 '14 at 12:50
  • Just formatting it as a question would be a large improvement. – Magus Jun 18 '14 at 14:34

1 Answers1

5

There are a number of steps involved in the conversion:

  1. Extract the individual octal numbers (such as 354) from the source string.
  2. Convert each octal string representation to its decimal equivalent as a byte.
  3. Decode the byte sequence as UTF-8.

Here's a sample implementation:

string source = @"\354\202\254\354\232\251\354\236\220\354\203\201\354\204" +
                @"\270\354\240\225\353\263\264\354\236\205\353\240\245";

byte[] bytes = source.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)
                     .Select(s => (byte)Convert.ToInt32(s, 8))
                     .ToArray();

string result = Encoding.UTF8.GetString(bytes);   // "사용자상세정보입력"
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • Thank you so much for the answer - especially the steps. That is what I was really looking for as I had #1 and I knew #3 would be re-encoding an array of numbers but #2 is where I was stuck - I couldn't figure out what to convert the numbers too. Given the commentary on the question I just want to say you have increased the knowledge of at least one very junior programmer.... – kdavej Jun 18 '14 at 12:58
  • @kdavej: You're welcome. And don't take the commentary personally; some of us here are more [lenient with newcomers](http://en.wikipedia.org/wiki/Wikipedia:BITE). – Douglas Jun 18 '14 at 18:40
  • Thank you - based on the sample above i could finally convert filenames reported by Git to actual filenames... :) – Dlanod Kcud Nov 19 '16 at 09:57