473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Encoding - OEM to ANSI

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
Nov 17 '05 #1
4 10217
> 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

Nov 17 '05 #2
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" <Ma***********@discussions.microsoft.com> schreef in
bericht news:6D**********************************@microsof t.com...
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

Nov 17 '05 #3
Yves Royer <yv********@telenetSPAM.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Ok.
Thanks a lot for the help and patience :-)

Yves
"Jon Skeet [C# MVP]" <sk***@pobox.com> schreef in bericht
news:MP************************@msnews.microsoft.c om...
Yves Royer <yv********@telenetSPAM.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mats-Lennart Hansson | last post by:
Hi, I want to get the ANSI value (0-255) of a character. Assume I create a character like this: string s = new string((char) 151, 1); Now I want to get the ANSI value (151) for this...
1
by: jj | last post by:
How do I programatically default the encoding of my "Notepad" into ANSI. My application uses notepad to save some text. Some of the client computers have UTF as the default encoding in their...
2
by: jmhmaine | last post by:
During the course of development cycle I receive HTML files from designers that use Macs and PCs, but use tools other then Visual Studio. So these files sometimes are not UTF-8 Encoded. I see...
4
by: Nick | last post by:
Hi, I am trying to output a string of chinese characters as a text file. When I open a file for writing from VB, the file is automatically set to UTF-8 encoding (can tell by opening the file...
11
by: LucaJonny | last post by:
Hi, I've got a problem using StreamReader in VB.NET. I try to read a txt file that contains extended characters and theese are removed from the line that is being read. I've read a lot of...
10
by: Mark Rae | last post by:
Hi, I'm in the process if converting the data out of an old DOS-based SunAccounts system (don't ask!) into SQL Server. The data has been sent to me as a collection of hundreds of SunAccounts...
37
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
4
by: WH | last post by:
Hi, I use a Streamreader in VB2005 to read text files I made before in NotePad, but it can't read characters like "é" or "ç". They are omitted, or replaced by other characters. I tried different...
1
by: Tejas | last post by:
Hi, I am using ldap_get_values() call to get the user attributes from LDAP. This call is returning the user attributes in UTF-8 encoding and its a PCHAR*. For normal English characters this...
4
by: Samuel | last post by:
Hi Currently text file that I create are UTF-8 encoded and I need ANSI encoding how can I do that Thank you, Samuel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.