Instead of replacing the space, you can use BigInteger and add “0” during the formatting step. This works because BigInteger considered integral.
String bin = str.chars() .mapToObj(Integer::toBinaryString) .map(BigInteger::new) .map(x -> String.format("%08d", x)) .collect(Collectors.joining(" "));
EDIT
As @Holger suggested, there is an easier solution that uses long instead of BigInteger , since the binary representation of Character.MAX_VALUE does not exceed the long limit.
String bin = str.chars() .mapToObj(Integer::toBinaryString) .mapToLong(Long::parseLong) .mapToObj(l -> String.format("%08d", l)) .collect(Collectors.joining(" "));
Flown
source share