Unicode to Ansi Conversion

G

Gino

I have written an application which uses a streamwriter to create which is
exported to a third party system. The third party system does not appear to
process the files in its original format. I have identified this as resulting
from the streamwriter outputing in Unicode.

If I open the text file using notepad and save it with the default 'ansi'
type the import works fine. How can I convert the initial file to ansi.

Thanks in advance for any replies!
 
G

Guru

Gino said:
I have written an application which uses a streamwriter to create which is
exported to a third party system. The third party system does not appear
to
process the files in its original format. I have identified this as
resulting
from the streamwriter outputing in Unicode.

If I open the text file using notepad and save it with the default 'ansi'
type the import works fine. How can I convert the initial file to ansi.

StreamWriter Constructor (String, Boolean, Encoding)

Dim MyEncoding As Encoding = Encoding.ASCII
Dim f As String = "C:\Temp\SomeFile.txt"
Dim sw As New StreamWriter(f, False, MyEncoding)

sw.Write(LoadsOfCrap)
sw.Close()
Thanks in advance for any replies!

Rude pig.
 
S

SurturZ

This is neater:

Dim strFilename As String = "C:\Temp\SomeFile.Txt"
Dim strTextToWrite As String = "this is the text to insert into the
file"
Using sw As New StreamWriter(strFilename, False,
System.Text.Encoding.ASCII)
sw.Write(strTextToWrite)
End Using

I forget how to turn off Byte Order Marks. Can't remember if Encoding.ASCII
emits them. If you get a couple of dud characters at the beginning of the
file, that's why.
 
B

Bill McCarthy

Hi David,

It's not only neater, it is more robust. A Using block encapsulates the
call to Dispose in a Finally block, thus ensuring the Dispose is called and
the unmanaged resources released. This is an example of when you should use
a Using block :)
 
C

Cor Ligthert[MVP]

Gino,

While answering the question from Lorenzo, I remebered that I once had
answered this questiong with this one.

Be aware that this is the American/Dutch code page (it is the only one with
the guilder character in it).

\\\
Dim Str As New StreamReader(FilePath)
Dim arrInput As Byte() = _
System.Text.Encoding.GetEncoding(437).GetBytes(Str.ReadToEnd)
Str.Close()
///

Cor
 
G

Gino

Thanks to all of you for taking time to respond.

I had tried to set the encoding both as ASCII and default on opening the
streamwriter to no avail. As I needed to create a fixed width delimited file
for import to the third party I was padding all fields to a required length.
On one string field I had padded with a null string ("" or string.empty, in
hind sight I know this doesn't make sense as you logically cannot pad with
something of no length) which in the writer output as a symbol (C
underscored with a squiggle). By replacing this with a space (" ") the text
file was suitably formatted.

I was confused by the fact that once the file was opened in notepad and
saved with the encoding option ANSI it resaved file to suitable format for
use by third party software..

Thanks again for responses and apologies to Guru or anyone who took offence
at for ' thanking you in advance' . I did intend this to sound sarcastic but
simply to account for replies in different timezones. I do appreciate the
time people voluntarily take in assisting on forums such as this.

Best regards
 
H

Herfried K. Wagner [MVP]

Guru said:
StreamWriter Constructor (String, Boolean, Encoding)

Dim MyEncoding As Encoding = Encoding.ASCII

ASCII != ANSI.

Use 'Encoding.Default' to use the system's default Windows ANSI codepage, or
use 'Encoding.GetEncoding(<codepage number>)' to get a specific Windows ANSI
codepage.
 
G

Guru

Herfried K. Wagner said:
ASCII != A<BITCHSLAP>

As if I didn't know, right? Of course, in your pinheaded way, you will be
totally oblivious as to why Encoding.ASCII was suggested. Now, run along and
be a good boy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top