1

I have a string

"\u00c7"

I can convert it in code;

char chr = Convert.ToChar("\u00c7"); // value of the chr is **Ç**

but I can't convert like this

I wrore \u00c7 in textbox1

char chr2 = Convert.ToChar(textbox1.Text); //This makes an error - number of character

I'm working on it for hours and can't find any solution.

7
  • this actually works if you run in a Console window I will post an example of what you can run in Console and you will see Commented Dec 9, 2017 at 23:10
  • If I understood you correctly you literally typed \u00c7 into the textbox? If so, that’s not the same as writing it this way in code as the backslash is an escape character. In code if you type \u this means that the next characters are a Unicode code point and that the compiler should convert the whole sequence to a single character. In a textbox you are literally writing a backslash followed by an u and some numbers/letters, so here it’s actually a sequence of six characters. The same would happen if you disable the escaping by writing "\\u00c7" or @"\u00c7" in code. Commented Dec 9, 2017 at 23:26
  • In general there is no particular reason that humans would type C# syntax into a textbox. Are you sure they'd want to type any computer language's literal string syntax? Why wouldn't they just type text the way they normally would with whatever keyboard, operating system and IME or other assistive utility they use? Commented Dec 10, 2017 at 17:17
  • @ckuri \u is for a UTF-16 code unit. C# doesn't have escapes for Unicode codepoints like JavaScript and HTML do. Commented Dec 10, 2017 at 17:31
  • Alternatively, you could accept HTML character entity references. Ç Ç 🚲 🚲 There are .NET libraries to decode those and they are more familiar to some users. Commented Dec 10, 2017 at 17:33

1 Answer 1

2

There is no inbuilt way to do this (escape sequences are only parsed as such at compile time), but its rather easy to parse the string yourself:

static bool TryParseAsChar(this string s, out char c)
{
    if (s != null)
    {
        if (s.Length == 1)
        {
            c = s[0];
            return true;
        }

        if (s.StartsWith("\\u", StringComparison.InvariantCulture) &&
            s.Length == 6)
        {

            var hex = s.Substring(2);

            if (int.TryParse(hex,
                             NumberStyles.AllowHexSpecifier,
                             CultureInfo.InvariantCulture,
                             out var i))
            {
                c = (char)i;
                return true;
            }
        }
    }

    c = default(char);
    return false;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Could be simplified to something like: ((char)(int.Parse(_String.Replace(@"\u", ""), NumberStyles.AllowHexSpecifier))).ToString(); A Unicode char may be represented by a Hex number longer that 4 digits (8 in effect).
@Jimi that doesn’t simplify anything, it has completely different semantics; It will only work correctly for valid escape sequences and blow up in any other case or give nonsensical results.
What do you mean by "nonsensical"? _String can be any Hex, represented in string, between 1 and FFFFFFFE, escaped or not. Returns the corresponding Unicode symbol. Of couse not all values can be translated to a representable string an some error handling is necessary. But it wasn't the point. I was saying that you are parsing strings only if escaped and of lenght = 6. So, if the input string is "\u41" (= "A") instead of "\u0041", it won't be parsed. Also, no (valid) values above FFFF will be parsed. Just to become annoying, in VS 2013 this method does not compile (i not declared).
@jimi you are missing the point. He is attempting to parse escaped sequences the compiler understands. `/u41’ is a compile time error. There is no such valid escaped sequence in C#
Of course not, but "\u41" is valid and you are not evaluating it, as are not evaluated Hex values above FFFF. If you think I'm saying "mine is better than yours", this is not the case. I noticed a "flaw" in your code and I thought to let you know about it. The code I posted was meant to explain the point, not as a "substitution" or a better way as it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.