Convert memoryStream to unicode string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
Im using .Net 1.1 with Vs2003
Im trying to convert memorystream that im getting from
the clipboard into a unicode string ex.

//Set The clipboard data (unicode text file)....
StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode);
sw.Write("Bla");
...
...
...

//Get the data from the clipboard
MemoryStream ms = dat.GetData( "Object Type" ) as System.IO.MemoryStream;

byte [] bytes = new byte[4096];
Encoding unicode = Encoding.Unicode;

while((count = ms.Read(bytes,0,4096)) > 0)
{
int result = unicode .GetCharCount(bytes, 0 ,count);
char [] chars = new char[result];
chars = unicode.GetChars(bytes, 0 , count);
//In This point the char array is not currect s.t unicode charecters are wrong
...
}

Same code is working fine with Encoding.ASCII
any known .Net 1.1 on this subject?

Thanks
 
Im using .Net 1.1 with Vs2003
Im trying to convert memorystream that im getting from
the clipboard into a unicode string ex.

You should use the Encoding class that actually matches how the
characters are encoded in the clipboard stream. If ASCII works and
Unicode doesn't then clearly they are not stored as UTF-16.

The conversion to unicode happens in the GetChars call regardless
which encoding class you're using, since all strings and chars in .NET
ae stored as Unicode.



Mattias
 
Back
Top