Convert DOS Cyrillic text to Unicode




Herfried K. Wagner [MVP]

* "Nikolay Petrov said:
How can I convert DOS cyrillic text to Unicode
Take a look at the 'System.Text.Encoding' class.

Cor Ligthert

Hi Nikolay,

Send some code in advance, when you have luck Jay will help you, you can
send this as well to the newsgroup.


There you have a change that Jon Skeet will help you.

They are the two who do the most encoding problems.




Jay B. Harlow [MVP - Outlook]

In addition to the other comments

What is the Code Page for DOS Cyrillic? Quickly checking MSDN I think its
866, but you need to double check!

You would use Encoding.GetEncoding to get the DOS Cyrillic Encoding object.

Imports System.Text

Dim cyrillic As Encoding = Encoding.GetEncoding(866)

Given an array of Bytes with DOS Cyrillic in it, you would use
Encoding.GetString to convert to a Unicode String.

Dim bytes() As Byte
Dim s As String = cyrillic.GetString(bytes)

Given a Unicode String, you would us Encoding.GetBytes to get an array of
Bytes with DOS cyrillic.

bytes = cyrillic.GetBytes(s)

If your DOS cyrillic is in a Text File you pass the Encoding object to your
System.IO reader & writer classes

Dim input As New StreamReader("myCyrillic.txt", cyrillic)

Dim output As New StreamWriter("myCyrillic.txt", False, cyrillic)

For information on Unicode, Encoding, and code pages (such as DOS Cyrillic)

One last thing: Once you have a String it is Unicode! Only Byte arrays &
Streams contain DOS Cyrillic and other character encodings.

Hope this helps

Nikolay Petrov

That was very helpfull.
But I have some problems. Let me first tell you exactly what I want to
I've made a simple ASP .NET page with two text boxes and a button.
What I need is, that a user paste DOS cyrillic text (taken from Notepad) in
left text box,
and when he clicks the button, the Converted to Unicode text to appear at
the right box.
So I get the DOS text as String, not as bytes. How should I proceed in this

Jay B. Harlow [MVP - Outlook]

What I need is, that a user paste DOS Cyrillic text (taken from Notepad) in
left text box,
I would expect Notepad will have Windows Cyrillic or Unicode or think it
has, depending on the version of Windows & your regional settings in Control
So I get the DOS text as String, not as bytes. How should I proceed in this
No you don't get DOS text as a String!

Strings in .NET are always Unicode! Period.

Notepad, the browser & ASP.NET has already converted your "DOS text" into
Unicode for you. As I stated Notepad made an assumption of what kind of text
it is, then the browser used some encoding, such as UTF-8 or Windows
Cyrillic to send the response to ASP.NET as a stream of bytes. ASP.NET then
converted this response stream of bytes into a Unicode String. Hence your
program now has a Unicode string!

I've only used the normal encoding for requests & response in ASP.NET, so
I'm not certain on how to use a specific encoding for requests & responses.

Unfortunately you will need to ask in one of the ASP.NET newsgroups, such as
microsoft.public.dotnet.framework.aspnet for specifics on specific encodings
on requests & responses...

Notice that in the above there is a whole lot of converting going on! Once
your user opened the file in Notepad it was converted, an assumption was
made about the type of text in the file (I strongly suspect the assumption
was not DOS Cyrillic). Then when you cut & pasted the text from notepad to
your browser a conversion may have been made, but more then likely it was
done in the code page of your regional settings in windows, then when you
submitted the page to ASP.NET a conversion is made from the request/response
encoding into Unicode. So by the time ASP.NET gets you text is has already
been converted for you, so it is no where near DOC Cyrillic any more.

If you have files with DOS Cyrillic in them and you need or want to use
ASP.NET to convert them to Unicode I would recommend rather then using a
notepad, a text box and cut & paste. That you use the input type=file HTML
control to upload your DOS Cyrillic to the server as a stream of bytes
(preserving the DOS Cyrillic), then using the encoding object as I showed to
read this stream validly converting it to Unicode.

Hope this helps



Nikolay Petrov

Definitely, Jay. Thank you!
I've got it working allready.
Thank you again.

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

Similar Threads