PowerShell's Write-Output, echo and output redirection insert excessive characters into output
The story
I'm writing a program that outputs a sequence of hex digits like does xxd -ps
command.
This output is intended to be converted into a binary file, e.g. with xxd -ps -r
.
When I try to invoke xxd -ps -r
on my hex output in PowerShell I don't get the desired binary, xxd's output is empty.
The problem
After some investigation I've revealed that PowerShell's Write-Output, it's alias echo and output redirection '>' — all they insert excessive characters like '\00' and some others into output.
The question
How to get rid of this behavior and wean Powershell from corrupting data flows between commands?
The test
I would like Powershell to behave like the good old cmd:
ps>java -cp HelloWorld > hello.txt
ps>xxd -ps hello.txt > hello.hex
ps>xxd -ps -r hello.hex
ps>HelloWorld
ps>
Currently the last command has empty output.
I'm ok
Now I will write my own hex to binary converter in java, which is ok for my project, even bet开发者_高级运维ter, but I think I will eventually stumble upon this problem in the future so I want to be prepared.
I've found something similar in the past. If the data is binary, redirection produces a corrupted output due to newline characters. I've resolved thanks to this good article.
精彩评论