Convert OEM to Unicode

F

Franky

I have a Command Prompt window open and select all the characters and copy
them to the clipboard.

I then read them from the clipboard
str = CType(DataO.GetData(DataFormats.OemText, False), String)

and try to convert them to unicode

Dim InEncoding As Encoding = Encoding.GetEncoding(437)

Dim OutEncoding As Encoding = Encoding.Unicode

Dim StrAsBytes As Byte() = InEncoding.GetBytes(str)

Dim OutEncodingBytes As Byte() = Encoding.Convert(InEncoding, OutEncoding,
StrAsBytes)

Dim OutEncodingChars As Char() = OutEncoding.GetChars(OutEncodingBytes)

textbox1.text= = New String(OutEncodingChars)

But they do not display correctly.

Can you see what is wrong?
 
F

Franky

I must have seen this example before - some place
because I have tested the statement
Encoding.GetEncoding( _

CultureInfo.InstalledUICulture.TextInfo.OEMCodePage _

).GetString(Encoding.Default.GetBytes(Text))

and it works .

My problem is that I must be misunderstanding something because the code I
submitted does not work and it seems to me that it should. I'd like to know
what is wrong with it.



Thanks



Herfried K. Wagner said:
Franky said:
I have a Command Prompt window open and select all the characters and copy
them to the clipboard.
[OEM to Unicode]

Take a look at this sample:

<URL:http://dotnet.mvps.org/dotnet/samples/misc/RedirectConsole.zip>
 
F

Franky

I can make it work but don't understand why.
Change from
Encoding.Convert(InEncoding, OutEncoding
to
Encoding.Convert(OutEncoding,InEncoding
Doc says
Public Shared Function Convert ( _
srcEncoding As Encoding, _
dstEncoding As Encoding, _
bytes As Byte() _
) As Byte()Here is the way I figure it.The string I get from the clipboard,
InStr, is OEM.So InStrAsBytes is an byte array, 1 byte per character.
OEMThat is the source array.and InEncoding is the source encoding.But to
make it work I have to treat it as the destination encoding.Unless the doc
is wrong.Anyone familiar enough with this to correct my analysis?ThanksI
have a Command Prompt window open and select all the characters and copy
them to the clipboard.

I then read them from the clipboard
InStr = CType(DataO.GetData(DataFormats.OemText, False), String)

and try to convert them to unicode

Dim InEncoding As Encoding = Encoding.GetEncoding(437)

Dim OutEncoding As Encoding = Encoding.Unicode

Dim InStrAsBytes As Byte() = InEncoding.GetBytes(InStr)

Dim OutEncodingBytes As Byte() = Encoding.Convert(InEncoding, OutEncoding,
InStrAsBytes)

Dim OutEncodingChars As Char() = OutEncoding.GetChars(OutEncodingBytes)

textbox1.text= = New String(OutEncodingChars)

But they do not display correctly.

Can you see what is wrong?
 
F

Franky

Scratch the last post. I did NOT work. So I'm back to square one. Why
doesn't the code at the bottom work? What is wrong with it?


....snip bad analysis...
Sorry
 

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