views:

121

answers:

2

Hi.

I am maintaining some Java code that I am currently converting to C#.

The Java code is doing this:

sendString(somedata + '\000');

And in C# I am trying to do the same:

sendString(somedata + '\000');

But on the '\000' VS2010 tells me that "Too many characters in character literal". How can I use '\000' in C#? I have tried to find out what the character is, but it seems to be " " or some kind of newline-character.

Do you know anything about the issue?

Thanks!

+6  A: 

'\0' will be just fine in C#.

What's happening is that C# sees \0 and converts that to a nul-character with an ASCII value of 0; then it sees two more 0s, which is illegal inside a character (since you used single quotes, not double quotes). The nul-character is typically not printable, which is why it looked like an empty string when you tried to print it.

What you've typed in Java is a character literal supporting an octal number. C# does not support octal literals in characters or numbers, in an effort to reduce programming mistakes.*

C# does supports Unicode literals of the form '\u0000' where 0000 is a 1-4 digit hexadecimal number.

* In PHP, for example, if you type in a number with a leading zero that is a valid octal number, it gets translated. If it's not a legal octal number, it doesn't get translated correctly. <? echo 017; echo ", "; echo 018; ?> outputs 15, 1 on my machine.

Mark Rushakoff
Heh... we wrote practically the same. +1
Mark Byers
Thanks too both of you, it seems to be working just fine :)
Kristoffersen
+4  A: 

That's a null character, also known as NUL. You can write it as '\0' in C#.

In C# the string "\000" represents three characters: the null character, followed by two zero digits. Since a character literal can only contain one character, this is why you get the error "Too many characters in character literal".

Mark Byers
And we're both named Mark... so +1 to you too.
Mark Rushakoff