How to replace spaces excluding those within double quotes and after backslash?

In bash or perl, I would like to know how to substitute a null character (0x00) for every white space without changing the white spaces inside the block of double quotes and the white space immediately following a backslash.

Suppose that sample.txt consists of the following line.

"b 1" c\ 2

The above line contains three spaces. However, I would like to replace only the space immediately before c.

The following command undesirably replaced all the three spaces, ignoring quotes and backslashes.

perl -p -e 's/ /\x00/g' sample.txt > null.txt

I need the special meaning of double quotes and backslash to be honored.

Other substitution tools I can think of are the tr and sed commands on bash. However, I do not even know how to substitute a null character using tr or sed.

Many thanks in advance.

something like this ?

$ echo "\"b 1\" c\ 2" | perl -pe 's/\" /\"\x00/g'
"b 1"c\ 2

No, that is not what I want. Do not rely on the coincidence that the space I want to remove happens to follow a closing double quote.