How do I read special characters from a txt file on SD card?
I have now managed to implement a font with our special Danish characters (in Glide). This works.
Now I want to read a simple text file from SD card. This works OK.
But not when reading special characters (ie æ, ø, å ).
When I read these from the text file, they simply turn up as some chinese things…
I have tried to investigate on this, but the NETMF seems to be somewhat limited in encoding, when reading from the file.
This is my soucre so far:
using (StreamReader srShoppingList = new StreamReader(fileShoppingList))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
iIndex = 0;
// This is a test to try to make some encoding (does not work):
byte[] bytes = Encoding.UTF8.GetBytes(srShoppingList.ReadLine());
string strDum = new string(Encoding.UTF8.GetChars( bytes ));
// Read all line in file, until END:
while ((line = srShoppingList.ReadLine()) != "END")
{
// AT THIS POINT, "line" DOES NOT READ THE SPECIAL CHARACTERS!!!!
// HOW DO I READ THEM?
strShoppingList[iIndex] = line;
iIndex++;
// Console.WriteLine(line);
}
}
I really hope someone can help me, as this is quite annoying…
I have spent 90% of my programming time on handling Danish language…
You are in the area of Globalization and Localization, which I have little knowledge.
The MF does not seem to have cultural support for the Danish Language. You might have
to add support. This is discussed in Jens Kuhner book “Expert .NET Micro Framework”.
But, as I said before, I have little knowledge in this area.
I hope we have another Danish member who can help ypu.
Eric: I use UTF8, but it does not work.
The ASCII example was found on a .NET Microframework site. And it should be a NETMF example code. Seems strange…
I have tried to make an ugly workaround:
This works:
string line = "Mælk";
Now, “line” contains the string “Mælk”, and shows OK on the display.
This does not work:
byte[] byteLine;
byteLine[0] = 'M';
byteLine[1] = 'æ';
byteLine[2] = 'l';
byteLine[3] = 'k';
byteLine[4] = 0;
// OK until now. Now I will convert the char/bytes to a string containing these characters:
string strDum = new string(Encoding.UTF8.GetChars(byteLine)); // CRASHES with an expression !!!!
The encoding simple does not like characters above ASCII 0xFF I guess.
Kåre, is it important in this application for the text file on the SD card to be Danish-readable on other systems (i.e. the card may be taken out and read on a PC), or is it only backing-store for the NETMF application (only your device will read the SD card, doesn’t need to be taken out)? If the second option, could maybe work around this problem by substituting character sequences before saving and swap them back after loading - so for instance swapping ‘æ’ for ‘{ae}’ before saving then when loading back swapping ‘{ae}’ for ‘æ’ again? That way the text file saves ‘M{ae}lk’ but you can read it back and display ‘Mælk’ on screen. Do the same for ‘å’ and ‘{aa}’ etc., should work.
I actually already are working on this ugly workaround :o)
No, I am not forced to be able to read the file from PC for this project.
But for future projects, I find this issue very confusing…
I am trying to use the StringBuilder. Some says it is not, and some says it should be available in future NETMF versions.
What I want is simpy to use StringBuilder here:
There is a “Remove” member I want to use.
So I add the reference to MFDpwsExtensenions. But the class does not hold the StringBuilder ???
Still really confused about NETMF! I find it more and more limited when working with it…
Can anyone tell me how to use StringBuilder?
When I google the problem, I am doing it the right way.
This is a key - the netmf is a CUT DOWN framework, some things that take a lot of program space are not included. Strings are “expensive” in both program and storage, so they’ve been “limited” in 4.1.
So you have a choice, if you need stringbuilder itself. Depending on what hardware you’re running, targetting a 4.2 app would give you stringbuilder (if your hardware has a 4.2 port).