5

If I do echo zzz > test.txt and then open test.txt what I see is this:

ÿþz z z 

But sometimes it's just desirable to be able to output single byte encodings. Like maybe the program is outputing a binary file, like a JPEG image, or some such.

Anyway, is there anyway to make it so redirected output in Powershell is done using a single byte encoding instead of a multibyte one?

1
  • 1
    The string, "ÿþz z z", is in UTF-16. You can tell from the byte order mark. Also, you can see that that "z" characters have an extra character between them, which suggests that the encoding is 2-byte.
    – dangph
    Aug 24, 2015 at 2:43

1 Answer 1

5

The following will write the string out in the current Windows ANSI code page. This is probably what you will usually want to do to output single-byte encoded strings.

echo zzz | Out-File -Encoding default test.txt

You could also use ASCII:

echo zzz | Out-File -Encoding ascii test.txt

To output a byte array, do something like this:

$myByteArray = New-Object Byte[] 100   # Array of 100 bytes.
[io.file]::WriteAllBytes('Test.dat', $myByteArray)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .