I need to escape the following sequence defined as a static final
final String POSIX_SIGNATURE = "ustar".concat("\0").concat("00");
How would I escape this without using the .concat()
method nor the +
string operator?
final String POSIX_SIGNATURE = "ustar\000";
This is not valid, not the same as the first one.
final String POSIX_SIGNATURE = "ustar\0\00";
Nor is this one either.
use C-strings that are null terminated. The leading string before NUL is interpreted as the string and the trailing string discarded. You can only input using pipes - printf '\0' | cat -v or letting the resulting program use a file for input.
The null character is often represented as the escape sequence \0 in source code , string literals or character constants.
In a C string, each character occupies one byte of memory including null character. Null character marks the end of the string, it is the only way for the compiler to know where this string is ending.
A null character is one byte and an unsigned int is two bytes.
Use a unicode escape sequence:
"\u000000"
This is the NUL character (\u0000
), followed by two 0's.
Darth Android has the solution, but here is the reason your attempts didn't work the way you expected.
The reason the null character is represented by \0
isn't because it's a special escape sequence just for the null character; it's because it's an octal escape sequence of up to 3 octal digits.
Your string "ustar\000"
has 1 null character on the end, \000
, and your string "ustar\0\00"
has 2 null characters on the end, \0
and \00
.
The solution is as previously mentioned by Darth Android -- use a Unicode escape, which has exactly 4 hex digits, so that extra 0
characters are just that -- 0
characters. The \u0000
is the null character, and the 5th and 6th 0
s are literal 0
characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With