Character constants

B

br

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
 
M

Maciej Kromrych

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

ms-help://MS.NETFrameworkSDK/csref/html/vclrfChar_PG.htm

HTH,
Maciej
 
J

Jon Skeet

br said:
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?

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

Simon Trew

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

Simon Trew

Yes, you are right.

Jon Skeet said:
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.WriteLine (s1.Length);
Console.WriteLine (s2.Length);
}
}

prints
4
3
as I'd expect it to.

Basically, VS.NET 2002 has some problems when it comes to debugging
strings.
 
M

Michael \(michka\) Kaplan [MS]

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 said:
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.
 
S

Simon Trew

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top