One of the comments under the question:
It could be that cat
is changing individual occurrences of LF
and CR
to the sequence CRLF
because it is intended for text.
Real cats are too lazy for this sh… shenanigan. In PowerShell cat
is an alias for Get-Content
. You need few parameters to make it behave like a nice cat
.
Example 6: Get raw content
The commands in this example get the contents of a file as one string, instead of an array of strings. By default, without the Raw
dynamic parameter, content is returned as an array of newline-delimited strings. […]
$raw = Get-Content -Path .\LineNumbers.txt -Raw
I guess in your case this "array of newline-delimited strings" was concatenated and some monkey business with line endings occurred. But even with Raw
you will still get a string. You don't want a string.
Example 8: Get file contents as a byte array
This example demonstrates how to get the contents of a file as a [byte[]]
as a single object.
$byteArray = Get-Content -Path C:\temp\test.txt -AsByteStream -Raw
[…]
The first command uses the AsByteStream
parameter to get the stream of bytes from the file. The Raw
parameter ensures that the bytes are returned as a [System.Byte[]]
. If the Raw
parameter was absent, the return value is a stream of bytes, which is interpreted by PowerShell as [System.Object[]]
.
(source)
I don't know PowerShell at all. I don't know if you should want [System.Byte[]]
or [System.Object[]]
, or if it matters at all in your case. I found this statement:
Combining -Raw
with -AsByteStream
/ -Encoding Byte
is actually the simplest way to get the file's content as a raw byte array ([byte[]]
), output as a single object (as opposed to the bytes streaming one by one, in the absence of -Raw
).
I guess probably you should use Raw
. I'm certain you should use AsByteStream
.
cat
is changing individual occurrences of LF and CR to the sequence CRLF because it is intended for text.