Force C# to use ASCII

后端 未结 4 1199
一整个雨季
一整个雨季 2021-01-05 21:27

I\'m working on an application in C# and need to read and write from a particular datafile format. The only issue at the moment is that the format uses strictly single byte

相关标签:
4条回答
  • 2021-01-05 21:41

    C# (.NET) will always use Unicode for strings. This is by design.

    When you read or write to your file, you can, however, use a StreamReader/StreamWriter set to force ASCII Encoding, like so:

    StreamReader reader = new StreamReader (fileStream, new ASCIIEncoding());
    

    Then just read using StreamReader.

    Writing is the same, just use StreamWriter.

    0 讨论(0)
  • 2021-01-05 21:45

    If you have a file format that mixes text in single-byte characters with binary values such as lengths, control characters, a good encoding to use is code page 28591 aka Latin1 aka ISO-8859-1.

    You can get this encoding by using whichever of the following is the most readable:

    Encoding.GetEncoding(28591) 
    Encoding.GetEncoding("Latin1")
    Encoding.GetEncoding("ISO-8859-1")
    

    This encoding has the useful characteristic that byte values up to 255 are converted to unchanged to the unicode character with the same value (e.g. the byte 0x80 becomes the character 0x0080).

    In your scenario, this may be more useful than the ASCII encoding (which converts values in the range 0x80 to 0xFF to '?') or any of the other usual encodings, which will also convert some of the characters in this range.

    0 讨论(0)
  • 2021-01-05 22:01

    Interally strings in .NET are always Unicode, but that really shouldn't be of much interest to you. If you have a particular format that you need to adhere to, then the route you went down (reading it as bytes) was correct. You simply need to use the System.Encoding.ASCII class to do your conversions from string->byte[] and byte[]->string.

    0 讨论(0)
  • 2021-01-05 22:04

    If you want this in .NET, you could use F# to make a library supporting this. F# supports ASCII strings, with a byte array as the underlying type, see Literals (F#) (MSDN):

    let asciiString = "This is a string"B
    
    0 讨论(0)
提交回复
热议问题