Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top