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!
'\0'will be just fine in C#.What’s happening is that C# sees
\0and converts that to a nul-character with an ASCII value of 0; then it sees two more0s, 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'where0000is 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; ?>outputs15, 1on my machine.