c# - Convert a file to UTF-8 without streamwriter -
is there way convert file stream data utf-8 file stream without making use of stream writer, of reading line line , writing utf-8 file, there faster way of converting file utf-8 encoding
using(streamwriter writer = new streamwriter(destinationfile, system.text.encoding.utf8)) { string line = ""; while ((line = reader.readline()) != null) { writer.writeline(line); } }
is there overload method in memory stream or filestream convert file utf8 encoded file
yes:
string text = file.readalltext(srcfilename); file.writealltext(dstfilename, text, system.text.encoding.utf8);
edit: reply request in comment
surrogates utf-8 characters require more 1 byte (at least 2 there may more). let's block 1024 bytes long (this problem arises block length, but: larger blocks are, less probability break surrogate). surrogate broken when spans across block boundary, shown here:
block index character comment 0 0 block start 0 1 b ... 0 1022 0 1023 € block end, character 3 bytes long --------------------- 1 1024 € (+1) second surrogate byte of character 1 1025 € (+2) third surrogate byte of character ...
as can see, three-byte character €
broken between 2 blocks. when streaming in/out block @ time, these cases have handled correctly in code.
for more examples , explanations actual codes see wikipedia, possibly not more thorough , precise are.
Comments
Post a Comment