Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping null byte next to two zeroes

Tags:

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.

like image 943
Gala Avatar asked Apr 11 '16 19:04

Gala


People also ask

How do you pass a null byte?

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.

Which escape sequence is used for null character?

The null character is often represented as the escape sequence \0 in source code , string literals or character constants.

Does null character occupy a byte?

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.

How many bytes is a null byte?

A null character is one byte and an unsigned int is two bytes.


2 Answers

Use a unicode escape sequence:

"\u000000" 

This is the NUL character (\u0000), followed by two 0's.

like image 120
Darth Android Avatar answered Oct 17 '22 02:10

Darth Android


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 0s are literal 0 characters.

like image 25
rgettman Avatar answered Oct 17 '22 04:10

rgettman