web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Cannot convert Text file to ANSI format

(1) ShareShare
ReportReport
Posted on by 333
Hi ,
 
I'm trying to create a plain text file in ANSI format. Please see my below code for reference. I used stream reader to check while debugging , but the current encoding still shows UTF8. I need the file in ANSI format. 
 
Please help.
 
 System.IO.Stream stream1 = streamIO.getStream();
 stream1.Position = 0;
 System.IO.StreamReader reader1 = new System.IO.StreamReader(stream1);
str filecontent = reader1.ReadToEnd();

 System.Text.Encoding encoding = System.Text.Encoding::GetEncoding(1252);

 System.Byte[] byteArray = encoding.GetBytes(fileContent);

 System.IO.Stream stream = new System.IO.MemoryStream(byteArray);
 stream.Position=0;
 
Initializing streamIO as below:
 streamIO = TextStreamIo::constructForWrite();

 streamIO.outFieldDelimiter("^");
 
 streamIO.inFieldDelimiter("$");
 
Thanks.
Categories:
I have the same question (0)
  • PriyaDutta Profile Picture
    333 on at
    Cannot convert Text file to ANSI format
    Hi Martin,
     
    When I add non ANSI characters like "€, Ã±" etc  and generate the file. Then the file in Notepad++ shows as ANSI. But when I removed these characters and generated the same file then it shows UTF8. Looks like the notepad++ is guessing the encoding and as you said its not accurate.
     
    Thanks for the help , I think it is working fine.
     
     
  • Martin Dráb Profile Picture
    235,914 Most Valuable Professional on at
    Cannot convert Text file to ANSI format
    Could you please tell me what you mean by "I see UTF8"? Where do you see it?
     
    Your code looks correct to me; byteArray contains the bytes of text in 1252 codepage.
     
    You can test that the text is in the correct codepage by including some characters above ASCII and trying to interpretting the data as different codepage (such as 1250), e.g. in Notepad++.
    Regarding UTF-8, it includes all characters of 1252 codepage, but you can use a character that isn't included in 1252. The expected result is getting a question mark instead of the character.
     
    By the way, I would use System.Text.Encoding::UTF8 rather than System.Text.Encoding::GetEncoding(65001), because the name is easier to understand than the code.
  • PriyaDutta Profile Picture
    333 on at
    Cannot convert Text file to ANSI format
    Hi Martin,
     
    Yes you are right its F&O.
     
    I also tried your suggestion and have written the below code. Still I see UTF8 only.
    As you said its difficult to check which encoding it is. How to know if the code is working or not ? 
     System.IO.Stream stream1 = streamIO.getStream();
     stream1.Position = 0;
     System.IO.StreamReader reader1 = new System.IO.StreamReader(stream1);
    str filecontent = reader1.ReadToEnd();
    
     System.Text.Encoding ansiEncoding = System.Text.Encoding::GetEncoding(1252);
     System.Text.Encoding utfEncoding = System.Text.Encoding::GetEncoding(65001);
                          
    
     System.Byte[] utf8Bytes = utfEncoding.GetBytes(fileContent);
    
     System.Byte[] byteArray = System.Text.Encoding::Convert(utfEncoding, ansiEncoding, utf8Bytes);
    
     System.IO.Stream stream = new System.IO.MemoryStream(byteArray);
     stream.Position=0;
     
     
    Thanks.
  • Verified answer
    Martin Dráb Profile Picture
    235,914 Most Valuable Professional on at
    Cannot convert Text file to ANSI format
    If fileContent is contains UTF8 data, reading it with Encoding for 1252  is wrong. You should get bytes for the right encoding and then use System.TextEncoding::Convert() to convert the data from one encoding to another.
     
    By the way, be careful with "but the current encoding still shows UTF8". Except of files with BOM, there usually is no information about encoding, therefore text editors can't really tell us which encoding it is. Then often show something, but you can't trust it.
      
    By the way, you've chosen Dynamics AX as the category, but the code looks like F&O. Isn't it the case?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 764 User Group Leader

#2
Martin Dráb Profile Picture

Martin Dráb 619 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 603 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans