Home > Software design >  Convert UTF-8 files to ANSI
Convert UTF-8 files to ANSI

Time:12-24

How to remove NUL string in the message box

System.IO.FileStream fs = System.IO.File.OpenRead(@"C:\Users\Admin\Downloads\VQ\NQCUEMES221222122249.txt");
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
fs.Close();
System.Text.Encoding inputEnc = System.Text.Encoding.UTF8;
System.Text.Encoding outputEnc = System.Text.Encoding.Default;
//System.Text.Encoding outputEnc = System.Text.Encoding.Unicode;
//System.Text.Encoding outputEnc = System.Text.Encoding.ASCII;
//System.Text.Encoding outputEnc = System.Text.Encoding.GetEncoding(1252);
byte[] decoded = System.Text.Encoding.Convert(inputEnc, outputEnc, bytes, 0, bytes.Length);
System.IO.FileStream fw = System.IO.File.OpenWrite(@"C:\Users\Admin\Downloads\VQ\NQCUEMES221222122249_NEW.txt");
fw.Write(decoded, 0, (int)decoded.Length);
fw.Close();
MessageBox.Show("END");

The correct conversion instead is done by NOTEPAD

With NUL: With NUL

Without NUL: Without NUL

CodePudding user response:

If the input files has NUL characters you could replace them by spaces after reading the input file using:

for (var i = 0; i < bytes.Length; i  )
{
    if (bytes[i] == '\0')
        bytes[i] = ' ';
}
  •  Tags:  
  • c#
  • Related