|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Encoding questionHi,
I have a string and I one to convert it to a different encoding, right now I first convert it to binary and then convert the binary to a string with the new encoding. That is: ' strText1: Original text ' strText2: text with new enconding (the enconding page number is intEncodingPage) Dim bytAux() As Byte = Encoding.Default.GetBytes(strText1) Dim strText2 as string = Encoding.GetEncoding(intEncodingPage).GetString(bytAux) Is there anyway to convert directly without converting to byte first? Thanks, jaime >I have a string and I one to convert it to a different encoding, right now I That doesn't quite make sense. Whenever you have a String object, its>first convert it to binary and then convert the binary to a string with the >new encoding. contents is Unicode (UTF-16 basically). So you're not converting to a string with a new encoding with your GetString call. What you're doing is saying that the data in the byte array can be interpreted as encoded with whatever encoding intEncodingPage represents, and requests that it's converted to a .NET String. Your current code could easily result in data loss if the byte array content isn't valid according to the encoding you're using. If you want to store the text in a specific encoding and not as a .NET UTF-16 String, you have to keep in in a byte array. So what you probably want to do is Dim encodedText() As Byte = Encoding.GetEncoding(intEncodingPage).GetBytes(strText1) Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Redim Multidimenional Arrays
MS Word Chart filled with data - URGENT PLEASE VB6 --> VB 2005 upgrade ...IDE now VERY slow!!! using strongly typed dataset update problem Importing DLLs during runtime Compiling .dll's into a stand-alone .exe FolderBrowserDialag? Problems with shortcut files Can't get the Internet Transfer Control to work Microsoft.VisualBasic.Replace bug |
|||||||||||||||||||||||