simple unicode question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Ireland

    #1

    simple unicode question

    Hi -

    It's funny ... this works fine:

    private void button2_Click(o bject sender, System.EventArg s e) {
    string s = "\u0075";
    char c = Convert.ToChar( s);
    label1.Text = c.ToString();
    }


    But now imagine I want to build the string s dynamically, e.g.

    private void button2_Click(o bject sender, System.EventArg s e) {
    string s1 = "00";
    string s2 = "75";
    string s = "\u" + s1 + s2;
    char c = Convert.ToChar( s);
    label1.Text = c.ToString();
    }

    The code as written above gives me a design-time error under "\u" which says
    "Unrecognis ed escape sequence". If I change "\u" to @"\u" I now get the
    following runtime exception:
    An unhandled exception of type 'System.FormatE xception' occurred in
    mscorlib.dll.

    Additional information: String must be only one character long.

    Can anybody please tell me how I can workaround this to dynamically build a
    string (like 's') and have it converted into a Unicode character?

    Thanks,

    Chris.


  • JPRoot

    #2
    Re: simple unicode question

    Hope this helps...

    static void Main(string[] args)
    {
    // The inputs
    string s1 = "00";
    string s2 = "75";

    // Now the conversion
    short unicodeVal = System.Convert. ToInt16(s1 + s2, 16);
    string unicodeCharWith inString = "" + System.Convert. ToChar(unicodeV al);

    System.Console. WriteLine(unico deCharWithinStr ing);
    System.Console. ReadLine();
    }

    JPRoot

    "Christophe r Ireland" <christopher@re movemee.ireland life.net> wrote in
    message news:OMAhW1UtDH A.2440@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi -
    >
    > It's funny ... this works fine:
    >
    > private void button2_Click(o bject sender, System.EventArg s e) {
    > string s = "\u0075";
    > char c = Convert.ToChar( s);
    > label1.Text = c.ToString();
    > }
    >
    >
    > But now imagine I want to build the string s dynamically, e.g.
    >
    > private void button2_Click(o bject sender, System.EventArg s e) {
    > string s1 = "00";
    > string s2 = "75";
    > string s = "\u" + s1 + s2;
    > char c = Convert.ToChar( s);
    > label1.Text = c.ToString();
    > }
    >
    > The code as written above gives me a design-time error under "\u" which[/color]
    says[color=blue]
    > "Unrecognis ed escape sequence". If I change "\u" to @"\u" I now get the
    > following runtime exception:
    > An unhandled exception of type 'System.FormatE xception' occurred in
    > mscorlib.dll.
    >
    > Additional information: String must be only one character long.
    >
    > Can anybody please tell me how I can workaround this to dynamically build[/color]
    a[color=blue]
    > string (like 's') and have it converted into a Unicode character?
    >
    > Thanks,
    >
    > Chris.
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: simple unicode question

      Christopher Ireland <christopher@re movemee.ireland life.net> wrote:[color=blue]
      > It's funny ... this works fine:
      >
      > private void button2_Click(o bject sender, System.EventArg s e) {
      > string s = "\u0075";
      > char c = Convert.ToChar( s);
      > label1.Text = c.ToString();
      > }[/color]

      Note that the middle line would be more simply written as

      char c = s[0];
      [color=blue]
      > But now imagine I want to build the string s dynamically, e.g.
      >
      > private void button2_Click(o bject sender, System.EventArg s e) {
      > string s1 = "00";
      > string s2 = "75";
      > string s = "\u" + s1 + s2;
      > char c = Convert.ToChar( s);
      > label1.Text = c.ToString();
      > }
      >
      > The code as written above gives me a design-time error under "\u" which says
      > "Unrecognis ed escape sequence".[/color]

      Absolutely. The "\uxxxx" business belongs to the compiler, not the
      runtime. It's the same as trying to do:

      string s = "\" + "n";

      and expecting to get a newline character.
      [color=blue]
      > If I change "\u" to @"\u" I now get the
      > following runtime exception:
      > An unhandled exception of type 'System.FormatE xception' occurred in
      > mscorlib.dll.[/color]
      ?[color=blue]
      > Additional information: String must be only one character long.
      >
      > Can anybody please tell me how I can workaround this to dynamically build a
      > string (like 's') and have it converted into a Unicode character?[/color]

      Well, what do you *actually* want to get the data as? Do you really
      have it as 2 2-character strings? If so, you'd use Byte.Parse twice
      (using the form which allows you to specify that they're hex numbers),
      shift the top byte left, and then or the results:

      char c = (char)((topByte << 8)|lowByte);

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Christopher Ireland

        #4
        Re: simple unicode question

        Hi Jon,

        Many thanks -- that was the ticket!!

        Chris.


        Comment

        Working...