Encoding - OEM to ANSI

Y

Yves Royer

Hi all,

i'm currently writing an application which can read multiple formats of
textfiles and converts all of those to another one and the same. One of the
incoming type of textfiles are written in the OEM codepage. My question is,
how can i convert the OEM textfile to a textfile using the ANSI (Windows)
codepage? I've read something about the Encoding and Encoder classes in the
System.Text namespace but i can't figure it out how to do it... :-/

Can anybody help me with this one?

Thanks,

Yves
 
G

Guest

Can anybody help me with this one?

The most direct way to do this would be to read the file into a byte array
and call OemToCharA (the ANSI version of the OemToChar Win32 API).

If you definitely want to use the Encoding classes I guess you could use the
GetOEMCP API to get the current OEM code page, construct an Encoding instance
from that, convert the OEM data to a Unicode string or char[], and then
finally back to an ANSI byte array with Encoding.Default. But this involves a
rather unnecessesary Unicode conversion as an intermediate step.

Note that there's not one OEM code page. There are a whole bunch of them and
which one GetOEMCP returns is system dependent.


Mattias
 
Y

Yves Royer

Thanks for your answer.
But the thing about the Win32 API... I don't know exactly how you can do it
that way... any example mayby? I'm sorry about the (silly) question but i'm
rather new to C#.

Thanks,

Yves


Mattias Sjögren said:
Can anybody help me with this one?

The most direct way to do this would be to read the file into a byte array
and call OemToCharA (the ANSI version of the OemToChar Win32 API).

If you definitely want to use the Encoding classes I guess you could use
the
GetOEMCP API to get the current OEM code page, construct an Encoding
instance
from that, convert the OEM data to a Unicode string or char[], and then
finally back to an ANSI byte array with Encoding.Default. But this
involves a
rather unnecessesary Unicode conversion as an intermediate step.

Note that there's not one OEM code page. There are a whole bunch of them
and
which one GetOEMCP returns is system dependent.


Mattias
 
M

Mattias Sjögren

Yves,
But the thing about the Win32 API... I don't know exactly how you can do it
that way... any example mayby? I'm sorry about the (silly) question but i'm
rather new to C#.

Untested, because I don't have any OEM encoded data to try it on.

[Dllimport("user32.dll")]
static extern bool OemToCharA(byte[] lpszSrc, byte[] lpszDst);

....

using (FileStream fs = new FileStream("oem.txt", FileMode.Open))
{
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
OemToCharA(data, data);
// ... write data to output file ...
}


Mattias
 
Y

Yves Royer

I see... but isn't that the "unmanaged" way of coding in .NET?

I came to another possible difficulty... is there a simple way to determine
if the file is made with one of the OEM codepages or the ANSI codepage? This
way I know how i should read a file (OEM codepage or ANSI codepage).

With your first reply I came to this solution:

using System;

using System.Text;

using System.IO;

using System.Globalization;

namespace OEM2ANSI_Convertor

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

FileStream fsR = new FileStream(@"D:/DOS Sample.txt",
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

StreamReader sr = new StreamReader(fsR,
Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage));

String text = "";

try

{

text = sr.ReadToEnd();

}

catch (Exception ex)

{

}

finally

{

sr.Close();

fsR.Close();

}

FileStream fsW = new FileStream(@"D:/DOS Sample_.txt",
FileMode.Create, FileAccess.Write, FileShare.Read);

StreamWriter sw = new StreamWriter(fsW);

try

{

sw.Write(text);

}

catch (Exception ex)

{

}

finally

{

sw.Close();

fsW.Close();

}

}

}

}


So the main part of the question is solved. I can write files in the ANSI
format instead of the OEM format.

Thanks for the effort :)

Yves


Mattias Sjögren said:
Yves,
But the thing about the Win32 API... I don't know exactly how you can do
it
that way... any example mayby? I'm sorry about the (silly) question but
i'm
rather new to C#.

Untested, because I don't have any OEM encoded data to try it on.

[Dllimport("user32.dll")]
static extern bool OemToCharA(byte[] lpszSrc, byte[] lpszDst);

...

using (FileStream fs = new FileStream("oem.txt", FileMode.Open))
{
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
OemToCharA(data, data);
// ... write data to output file ...
}


Mattias
 
J

Jon Skeet [C# MVP]

Yves Royer said:
I see... but isn't that the "unmanaged" way of coding in .NET?

I came to another possible difficulty... is there a simple way to determine
if the file is made with one of the OEM codepages or the ANSI codepage? This
way I know how i should read a file (OEM codepage or ANSI codepage).

No, there isn't. The file is just a sequence of bytes. It could be
valid in either code page.
 

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