0

Take the following example:

string testfile1 = Path.Combine(HttpRuntime.AppDomainAppPath, "folder\\" + "test1.txt");
if (!System.IO.File.Exists(testfile1))
{
    System.IO.File.WriteAllText(testfile1, "£100", System.Text.Encoding.ASCII);
}

string testfile2 = Path.Combine(HttpRuntime.AppDomainAppPath, "folder\\" + "test2.txt");
if (!System.IO.File.Exists(testfile2))
{
    System.IO.File.WriteAllText(testfile2, "£100", System.Text.Encoding.UTF8);
}

Note the encoding. The first outputs ?100. The second outputs £100.

I know the encoding is different, but can somebody explain why ASCII encoding can't write the £ sign?

Captain_Planet
  • 1,228
  • 1
  • 12
  • 28
  • 2
    Hehe, the "£" glyph is only ever in the ASCII character set when you live in England. Sorry, the A in ASCII means "American". Consider "$". – Hans Passant Jun 22 '17 at 22:10
  • Unless you are following a specification that specifically calls for ASCII, it is seldom the right choice. (And, in many uses of the term "ASCII", the ASCII character set isn't even what's actually meant.) Consider the UTF-8 encoding for the [Unicode](http://www.unicode.org/charts/nameslist/index.html) character set—after all .NET, Java, JavaScript,… strings are Unicode. HTML XML, JSON, Java, C#,… files are Unicode (once decoded, anyway). But whichever of the many dozens of encodings you chose as the file writer, you have to tell your readers which one to read the text file with. – Tom Blodget Jun 22 '17 at 23:02
  • 1
    Oh, and instead of `Encoding.ASCII`, try this: `Encoding.GetEncoding("US-ASCII", EncoderExceptionFallback.ExceptionFallback, DecoderExceptionFallback.ExceptionFallback)`. (I much prefer exceptions to silent data loss, or in your case the loss of £100.) – Tom Blodget Jun 22 '17 at 23:03
  • Lots of nice answers on here - thank you all. Unfortunately it's a requirement to be in ASCII format (don't ask!) so I'll have some further discussion around this with the vendor! Thanks again! – Captain_Planet Jun 23 '17 at 05:15
  • Edited my answer for an idea for a solution for your requirement of using ASCII. – ispiro Jun 23 '17 at 11:25

3 Answers3

3

ASCII doesn't include the "£" character. That is - there is no byte value (nor a multiple byte value - they don't exist in ASCII) that denotes that symbol. So it shows you a "?" to tell you that. UTF8, on the other hand, does include it.

See here a list of all of the printable characters in ASCII.

If you must use ASCII, consider using "GBP" as mentioned here for Pound sterling. (Also might be relevant: Extended ASCII.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
1

To deal with ASCII and certain characters it depended largely on what code page you're using. £ isn't a character that is required or used universally within the latin alphabet so didn't appear in the standard ASCII set.

Look at this article or this one on code pages to see how the character limitation was resolved and for an idea as to why it won't show up everywhere.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
1

As Hans pointed out, ASCII is designed to Americans using only code points 0-127, the negligible rest of the English speaking world can live with that unless they try to use obscure symbols like £ with code points outside the range 0-127. I presume you live in the UK and aim only at customers from the UK, or Western Europe. Don't use Encoding.ASCII but Encoding.Default which would be code page 1252 in the UK, not in Turkey of course. You get real ASCII for every character in the ASCII range 0-127 but can also use characters in the range 128-255 where the pound symbol lives. But note, if someone tries to read the file assuming it is encoded in UTF8, the £ sign will obscure the content since it includes a byte that is non-existing in UTF8. This is indicated by some weird glyph like �.