c# - Convert a String, which is already malformed -
i have class, uses class reads textfile. textfile written in ascii or clear cp1525.
background info: the textfile generated in axapta , uses asciiio class writes text using writeraw method
the class using collegue , using c# streamreader read files. works okay because files written in utf8, in particular case isn't.
so streamreader reads file utf8 , passes read string me. have letters, example lating small letter o diaeresis (ö) aren't formated need them be.
a simple convert of string doesn't in case , can't figure out how can right letters.
so how reads it:
char quotationchar = '"'; string line = ""; using (streamreader reader = new streamreader(filename)) { if((line = reader.readline()) != null) { line = line.replace(quotationchar.tostring(), ""); } } return line;
what happens is, in textfile have german word "röhre" which, after reading streamreader, transforms r�hre (which looks stupid in database).
i try convert every letter
encoding enc = encoding.getencoding(1252); byte[] utf8_bytes = new byte[line.length]; (int = 0; < line.length; ++i) { utf8_bytes[i] = (byte)line[i]; } string propencodestring = enc.getstring(utf8_bytes, 0, utf8_bytes.length);
that doesn't give me right character !
byte[] myarr = encoding.utf8.getbytes(line); string propencodestring = enc.getstring(myarr);
that returns wrong character.
i aware solve problem using this:
using (streamreader reader = new streamreader(filename, encoding.default, true))
but fun: how can right string wrongly decoded string ?
once utf8 ascii conversion first made, characters don't map valid ascii entries replaced same bad data character means data lost , can't 'convert' character downstream. see example: https://dotnetfiddle.net/xwysml
Comments
Post a Comment