Java char literal for C # char literal

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

Java code does this:

sendString(somedata + '\000');

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

sendString(somedata + '\000');

But on `\ 000 ', VS2010 tells me that" There are too many characters in a literal literal. " How can I use '\ 000' in C #? I tried to figure out what kind of character this is, but he seems to be "or some kind of newline character."

Do you know anything about the problem?

Thanks!

+4
source share
2 answers

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

What happens is that C # sees \0 and converts it to nul-character with an ASCII value of 0; then he sees two more 0 s, which is illegal inside the character (since you used single quotes, not double quotes). Nul-character is usually not printed, so it looked like an empty line when you tried to print it.

What you typed in Java is a character literal supporting octal . C # does not support octal literals in characters or numbers to reduce programming errors. *

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

* In PHP, for example, if you enter a number with a zero number, which is a real octal number, it will be translated. If this is not a legal octal number, it will not be translated correctly. <? echo 017; echo ", "; echo 018; ?> <? echo 017; echo ", "; echo 018; ?> prints 15, 1 to my car.

+6
source

This is the null character , also known as NUL. You can write it as '\ 0' in C #.

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

+4
source

Source: https://habr.com/ru/post/1308593/


All Articles