Bye bye Ansi2Ascii (and welcome unicode)

Until NAV2013 I think I’ve used ANSI/ASCII conversion in pretty much every project, and Unicode has been a no-no. From NAV2013 we can use .Net to take care of conversions.

In this example I support DOS-PC8, Codepage 1252 and UTF-8 when importing a textfile. The format is specified using an option field in a setup table. You can of course do differently but I think you get the idea.

Name          DataType  Subtype Length
streamReader  DotNet    System.IO.StreamReader.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
streamWriter  DotNet    System.IO.StreamWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
encoding      DotNet    System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
InStr         InStream		
OutStr        OutStream		
ImportFile    File		
FileData      Text              250


ImportFile.TEXTMODE(TRUE);
ImportFile.OPEN([MyPathAndFilename]);
ImportFile.CREATEINSTREAM(InStr);
TempBlob.Blob.CREATEOUTSTREAM(OutStr);
COPYSTREAM(OutStr, InStr);
ImportFile.CLOSE;

TempBlob.Blob.CREATEINSTREAM(InStr);

CASE [MySetupTable]."File Encoding" OF
  0: //DOS-PC8
    streamReader := streamReader.StreamReader(InStr, encoding.GetEncoding(850));
  1: //ANSI
    streamReader := streamReader.StreamReader(InStr, encoding.GetEncoding(1252));
  2: //UTF8
    streamReader := streamReader.StreamReader(InStr, encoding.UTF8);
END;

WHILE NOT streamReader.EndOfStream DO
  BEGIN
    FileData := streamReader.ReadLine();
    //Do something with the data
  END

Tags: ,

One Response to “Bye bye Ansi2Ascii (and welcome unicode)”

  1. WordPress › Error

    There has been a critical error on your website.

    Learn more about debugging in WordPress.