Search code examples
c#escapingoctalstring-conversion

I have a string of octal escapes that I need to convert to Korean text - not sure how


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!


Solution

  • 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);   // "사용자상세정보입력"