Reputation: 53
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!
Upvotes: 4
Views: 809
Reputation: 258408
'\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 0
s, 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.
Upvotes: 6
Reputation: 839034
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".
Upvotes: 4