Character constants

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

    #1

    Character constants

    I need to define a constant for a character in my
    Constant class.

    I defined as follows
    public const char = '\136';

    when I compile I get the error 'Unrecognized escape
    sequence'

    How do I define a char constant with octal or hexadecimal
    values?

    Thanks
  • Maciej Kromrych

    #2
    Re: Character constants

    Check this (provided you have the Framework help installed):

    ms-help://MS.NETFramework SDK/csref/html/vclrfChar_PG.ht m

    HTH,
    Maciej


    Comment

    • Jon Skeet

      #3
      Re: Character constants

      br <sennai81@aol.c om> wrote:[color=blue]
      > I need to define a constant for a character in my
      > Constant class.
      >
      > I defined as follows
      > public const char = '\136';
      >
      > when I compile I get the error 'Unrecognized escape
      > sequence'
      >
      > How do I define a char constant with octal or hexadecimal
      > values?[/color]

      I don't know about octal (I don't think it's supported), but for hex
      you use either
      \uxxxx or \Uxxxxxxxx

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

      • Simon Trew

        #4
        Re: Character constants

        Hmm....

        I thought I'd see if the C-style escape sequences were available
        (deliberately not looking in the spec) so I tried:

        string s1 = "\0x40"; // in C you'd expect this to be U+0040 = "@"
        string s2 = "\040"; // In C you'd expect this to be U+0020 = " "

        This compiled correctly, but when run, both strings were reported to be of
        length 0! How can you explain this? Is the first '\0' interepreted as EOS? I
        didn't think strings were EOS-terminated in .NET (of course on the most part
        there's no need to know about the internal representation of string but I
        thought they used the COM BSTR encoding.)

        Trying this:

        char c1 = '\0x40';
        char c2 = '\040';

        In both cases this produced the error 'CS1012: Too many characters in string
        literal'. So I inferred that in the 'string' example the "[x]40" after the
        "\0" are being interpreted as separate characters, and the "\0" indicates
        EOS.

        Sure enough, the lines:

        string s1 = "\0x40";
        System.Console. WriteLine("{0}" , s1.Substring(1) );

        write out "0x40".

        Is this a bug in String.Length?

        S.

        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.1988ad 23449625e798a1c 7@news.microsof t.com...[color=blue]
        > br <sennai81@aol.c om> wrote:[color=green]
        > > I need to define a constant for a character in my
        > > Constant class.
        > >
        > > I defined as follows
        > > public const char = '\136';
        > >
        > > when I compile I get the error 'Unrecognized escape
        > > sequence'
        > >
        > > How do I define a char constant with octal or hexadecimal
        > > values?[/color]
        >
        > I don't know about octal (I don't think it's supported), but for hex
        > you use either
        > \uxxxx or \Uxxxxxxxx
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet/
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Simon Trew

          #5
          Re: Character constants

          I should add I'm running v1.0 of the framework.

          S.


          Comment

          • Simon Trew

            #6
            Re: Character constants

            Yes, you are right.

            "Jon Skeet" <skeet@pobox.co m> wrote in message
            news:MPG.1989b3 fc162e4d9098a1d 4@news.microsof t.com...[color=blue]
            > Simon Trew <ten.egnaro@wer ts> wrote:[color=green]
            > > I thought I'd see if the C-style escape sequences were available
            > > (deliberately not looking in the spec) so I tried:
            > >
            > > string s1 = "\0x40"; // in C you'd expect this to be U+0040 = "@"
            > > string s2 = "\040"; // In C you'd expect this to be U+0020 = " "
            > >
            > > This compiled correctly, but when run, both strings were reported to be[/color][/color]
            of[color=blue][color=green]
            > > length 0![/color]
            >
            > Is that in the debugger in VS.NET 2002 by any chance? It's not so from
            > a normal console app:
            >
            > using System;
            >
            > public class Test
            > {
            > static void Main()
            > {
            > string s1 = "\0x40";
            > string s2 = "\040";
            > Console.WriteLi ne (s1.Length);
            > Console.WriteLi ne (s2.Length);
            > }
            > }
            >
            > prints
            > 4
            > 3
            > as I'd expect it to.
            >
            > Basically, VS.NET 2002 has some problems when it comes to debugging
            > strings.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet/
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Michael \(michka\) Kaplan [MS]

              #7
              Re: Character constants

              Ths issue here is simple -- C-style escapes do not work here, and are not
              expected to.

              The "\0" is a single NULL character, so the counts you get are expected.


              --
              MichKa [MS]

              This posting is provided "AS IS" with
              no warranties, and confers no rights.


              "Simon Trew" <ten.egnaro@wer ts> wrote in message
              news:%23P%23UfF cUDHA.1776@TK2M SFTNGP09.phx.gb l...[color=blue]
              > Hmm....
              >
              > I thought I'd see if the C-style escape sequences were available
              > (deliberately not looking in the spec) so I tried:
              >
              > string s1 = "\0x40"; // in C you'd expect this to be U+0040 = "@"
              > string s2 = "\040"; // In C you'd expect this to be U+0020 = " "
              >
              > This compiled correctly, but when run, both strings were reported to be of
              > length 0! How can you explain this? Is the first '\0' interepreted as EOS?[/color]
              I[color=blue]
              > didn't think strings were EOS-terminated in .NET (of course on the most[/color]
              part[color=blue]
              > there's no need to know about the internal representation of string but I
              > thought they used the COM BSTR encoding.)
              >
              > Trying this:
              >
              > char c1 = '\0x40';
              > char c2 = '\040';
              >
              > In both cases this produced the error 'CS1012: Too many characters in[/color]
              string[color=blue]
              > literal'. So I inferred that in the 'string' example the "[x]40" after the
              > "\0" are being interpreted as separate characters, and the "\0" indicates
              > EOS.
              >
              > Sure enough, the lines:
              >
              > string s1 = "\0x40";
              > System.Console. WriteLine("{0}" , s1.Substring(1) );
              >
              > write out "0x40".
              >
              > Is this a bug in String.Length?
              >
              > S.
              >
              > "Jon Skeet" <skeet@pobox.co m> wrote in message
              > news:MPG.1988ad 23449625e798a1c 7@news.microsof t.com...[color=green]
              > > br <sennai81@aol.c om> wrote:[color=darkred]
              > > > I need to define a constant for a character in my
              > > > Constant class.
              > > >
              > > > I defined as follows
              > > > public const char = '\136';
              > > >
              > > > when I compile I get the error 'Unrecognized escape
              > > > sequence'
              > > >
              > > > How do I define a char constant with octal or hexadecimal
              > > > values?[/color]
              > >
              > > I don't know about octal (I don't think it's supported), but for hex
              > > you use either
              > > \uxxxx or \Uxxxxxxxx
              > >
              > > --
              > > Jon Skeet - <skeet@pobox.co m>
              > > http://www.pobox.com/~skeet/
              > > If replying to the group, please do not mail me too[/color]
              >
              >[/color]


              Comment

              • Simon Trew

                #8
                Re: Character constants

                I think you misunderstood-- the length was reported as 0 in the debugger,
                which was NOT what I was expecting if the string was allowed to contain
                embedded nulls. This is because of a bug in the VS2002 debugger; the actual
                string length is 4 as I DID expect.

                S.

                "Michael (michka) Kaplan [MS]" <michkap@online .microsoft.com> wrote in
                message news:ObE1WFrUDH A.1812@TK2MSFTN GP11.phx.gbl...[color=blue]
                > Ths issue here is simple -- C-style escapes do not work here, and are not
                > expected to.
                >
                > The "\0" is a single NULL character, so the counts you get are expected.
                >
                >
                > --
                > MichKa [MS][/color]



                Comment

                Working...