开发者

How can I determine the index in codepage 850 for a char in C#?

开发者 https://www.devze.com 2023-04-01 17:50 出处:网络
I have a text file which is encoded with codepage 850. I am reading this file the following way: using (var reader = new StreamReader(filePath, Encoding.GetEncoding(850)))

I have a text file which is encoded with codepage 850. I am reading this file the following way:

using (var reader = new StreamReader(filePath, Encoding.GetEncoding(850)))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        //...
    }
    //...
}

Now I need for every character in the string line in the loop above the zero-based index of that character which it has in codepage 850, something like:

for (int i = 0; i <开发者_高级运维; line.Length; i++)
{
    int indexInCodepage850 = GetIndexInCodepage850(line[i]); // ?
    //...
}

Is this possible and how could int GetIndexInCodepage850(char c) look like?


Use Encoding.GetBytes() on the line. CP850 is an 8-bit encoding, so the byte array should have just as many elements as the string had characters, and each element is the value of the character.


Just read the file as bytes, and you have the codepage 850 character codes:

byte[] data = File.ReadAllBytes(filePath);

You don't get it separated into lines, though. The character codes for CR and LF that you need to look for in the data are 13 and 10.


You don't need to.

You are already specifying the encoding in the streamreader constructor. The string returned from reader.ReadLine() will already have been encoding using CP850

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号