2

Running this command:

echo "foo" > test.txt

I get strange results in test.txt. I actually can't even copy-paste the contents directly here in stack overflow but need to show the hex output of the file. Which looks like this-

ff fe 66 00 6f 00 6f 00 0d 00 0a 00

Not sure what FF and FE are but it looks like it's also placing NULL between each character.

Can any Windows people guide me in the right direction as to why this is happening and how I can resolve it? I just want to the contents "foo" to be placed in that file unmolested.

1 Answer 1

8

0xFFFE is the Unicode byte order mark for UTF-16 LE (little endian), which is normal Windows Unicode.

The contents of your file are the two byte BOM, 0x6600 ("f"), 0x6f00 ("o"), 0x6f00 ("o"), 0x0d00 ("`r" or carriage return), 0x0a00 ("`n" or new line)

Try:

echo "foo" | Out-File -FilePath test.txt -Encoding utf8
Sign up to request clarification or add additional context in comments.

3 Comments

Ah I see. So those aren't nulls, they are just the extra length given it's 16bit encoded. Thanks!
Yep. You can kind of confirm what's going on like this: [System.Text.Encoding]::Unicode.GetString([byte[]](0xff,0xfe,0x66,0x00,0x6f,0x00,0x6f,0x00,0x0d,0x00,0x0a,0x00)).
Could also use Set-Content which uses ASCII/ANSI/whatever as default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.