ilyaigpetrov
ilyaigpetrov

Reputation: 3883

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 better, but I think I will eventually stumble upon this problem in the future so I want to be prepared.

Upvotes: 5

Views: 2041

Answers (1)

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

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.

Upvotes: 3

Related Questions