Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java char literal to C# char literal

Tags:

java

c#

character

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!

like image 445
Kristoffersen Avatar asked Dec 28 '22 18:12

Kristoffersen


1 Answers

'\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.

like image 184
Mark Rushakoff Avatar answered Dec 31 '22 14:12

Mark Rushakoff