Home All Groups Group Topic Archive Search About

Convert OEM to Unicode

Author
18 Dec 2006 4:01 PM
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?

Author
18 Dec 2006 4:07 PM
Herfried K. Wagner [MVP]
" Franky" <frankieNOSPAM@a-znet.com> schrieb:
>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>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
18 Dec 2006 4:35 PM
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



Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:eajvo6rIHHA.2232@TK2MSFTNGP02.phx.gbl...
>" Franky" <frankieNOSPAM@a-znet.com> schrieb:
>>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>
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
19 Dec 2006 8:05 PM
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?
Author
19 Dec 2006 8:31 PM
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


Show quoteHide quote
> 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
> 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?
>
>