473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Character constants

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
Nov 15 '05 #1
7 15031
Check this (provided you have the Framework help installed):

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

HTH,
Maciej
Nov 15 '05 #2
br <se******@aol.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
br <se******@aol.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #4
I should add I'm running v1.0 of the framework.

S.
Nov 15 '05 #5
Yes, you are right.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Simon Trew <ten.egnaro@werts> wrote:
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!


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #6
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@werts> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
br <se******@aol.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #7
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]" <mi*****@online.microsoft.com> wrote in
message news:Ob**************@TK2MSFTNGP11.phx.gbl...
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]


Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Akhil | last post by:
Since a character constant is an int value represented as a character in single quotes,so it is treated as a 1 byte integer now look at the following snippet. #include<stdio.h> int main(void)...
3
by: myjunk12 | last post by:
Hi, In C++, I could create an enum like this: enum Position { Quarterback = 'QB', LeftCornerback = 'LCB', RightOutsideLinebacker = 'ROLB' }
2
by: Kavya | last post by:
In C, the character constants are of type int but in C++ character constant are of type char. Why is there an incompatibilty here when C++ was designed to be a lot compatible to C. Shouldn't such...
2
by: Mirco Wahab | last post by:
After reading through some (open) Intel (CPU detection) C++ source (www.intel.com/cd/ids/developer/asmo-na/eng/276611.htm) I stumbled upon a sketchy use of multibyte characters - - - - - - - - -...
2
by: saranraj17 | last post by:
in c language how the character constant are stored in memory map.. in need very brief explanation . so kindly help for me
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.