Domanda

I need to export some data using Powershell to a ASCII encoded file.

My problem is that Scandinavian characters like Æ, Ø and Å turns into ? ? ? in the output file.

Example:

$str = "ÆØÅ" $str | Out-File C:\test\test.txt -Encoding ascii

In the output file the result of this is: ???

È stato utile?

Soluzione

It seems as though you have conflicting requirements.

  1. Save the text in ASCII encoding
  2. Save characters outside the ASCII character range

ASCII encoding does not support the characters you mention, which is the reason they do not work as you expect them to. The MSDN documentation on ASCII Encoding states that:

ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F.

And also further that

If your application requires 8-bit encoding (which is sometimes incorrectly referred to as "ASCII"), the UTF-8 encoding is recommended over the ASCII encoding. For the characters 0-7F, the results are identical, but use of UTF-8 avoids data loss by allowing representation of all Unicode characters that are representable. Note that the ASCII encoding has an 8th bit ambiguity that can allow malicious use, but the UTF-8 encoding removes ambiguity about the 8th bit.

You can read more about ASCII encoding on the Wikipedia page regarding ASCII Encoding (this page also includes tables showing all possible ASCII characters and control codes).

You need to either use a different encoding (such as UTF-8) or accept that you can't use characters which fall outside the ASCII range.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top