Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read unicode char from large textfile

Tags:

c#

unicode

How to read the unicode char? like "ä"

public static string Read(int length, string absolutePath)
{
    StringBuilder resultAsString = new StringBuilder();

    using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateFromFile(absolutePath))
    using (MemoryMappedViewStream memoryMappedViewStream = memoryMappedFile.CreateViewStream(0, length))
    {
        for (int i = 0; i < length; i++)
        {                  
            int result = memoryMappedViewStream.ReadByte();

            if (result == -1)
            {
                break;
            }
            char letter = (char)result;

            resultAsString.Append(letter);
        }
    }

    return resultAsString.ToString();
}

the read int (result) is 195 and the char cast gives me not the expected result.

like image 276
mty Avatar asked Aug 01 '26 07:08

mty


1 Answers

Not sure if that's what you ask for, but, you can use StreamReader

StreamReader sr = new StreamReader(stream, Encoding.Unicode);
like image 197
Denis Wasilew Avatar answered Aug 03 '26 21:08

Denis Wasilew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!