By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Encoding.GetEncode Problem

P: n/a
I have Windows Form application recieved data from clipboard and convert its
encoding based on some ruls. So doing following:
//from source[source] to multiple targets[targ1,targ2]
System.Text.Encoding targ1 = Encoding.GetEncoding("Target_1_code_name");
System.Text.Encoding targ2 = Encoding.GetEncoding("Target_2_code_name");
System.Text.Encoding source = Encoding.GetEncoding("Source_code_name");
byte[] sourceBytes = source.GetBytes(source);
byte[] targ1Bytes = Encoding.Convert(source, targ1, sourceBytes, 0,
sourceBytes.Length);
byte[] targ2Bytes = Encoding.Convert(source, targ2, sourceBytes, 0,
sourceBytes.Length);
string targ1String = source.GetString(targ1Bytes,0,targ1Bytes.Length);
string targ2String = source.GetString(targ2Bytes,0,targ2Bytes.Length);

BUT Nothing happens....(the source strings continue to be looking like it
was, target undepended). e.g.targ1String = targ2String =source;

What can be a problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
Nov 16 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi,

Strings (I mean System.String) are always Unicode - hence targ1String ==
targ2String == source.
But if you compare sourceBytes, targ1Bytes and targ2Bytes, these should be
different.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:Ov**************@TK2MSFTNGP10.phx.gbl...
I have Windows Form application recieved data from clipboard and convert
its
encoding based on some ruls. So doing following:
//from source[source] to multiple targets[targ1,targ2]
System.Text.Encoding targ1 = Encoding.GetEncoding("Target_1_code_name");
System.Text.Encoding targ2 = Encoding.GetEncoding("Target_2_code_name");
System.Text.Encoding source = Encoding.GetEncoding("Source_code_name");
byte[] sourceBytes = source.GetBytes(source);
byte[] targ1Bytes = Encoding.Convert(source, targ1, sourceBytes, 0,
sourceBytes.Length);
byte[] targ2Bytes = Encoding.Convert(source, targ2, sourceBytes, 0,
sourceBytes.Length);
string targ1String = source.GetString(targ1Bytes,0,targ1Bytes.Length);
string targ2String = source.GetString(targ2Bytes,0,targ2Bytes.Length);

BUT Nothing happens....(the source strings continue to be looking like it
was, target undepended). e.g.targ1String = targ2String =source;

What can be a problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "


Nov 16 '05 #2

P: n/a
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
I have Windows Form application recieved data from clipboard and convert its
encoding based on some ruls.


If you've received the data as a string, you don't need to do any
encoding conversion. It's already in Unicode.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
So how to do it?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:ey**************@TK2MSFTNGP10.phx.gbl...
Hi,

Strings (I mean System.String) are always Unicode - hence targ1String ==
targ2String == source.
But if you compare sourceBytes, targ1Bytes and targ2Bytes, these should be
different.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:Ov**************@TK2MSFTNGP10.phx.gbl...
I have Windows Form application recieved data from clipboard and convert
its
encoding based on some ruls. So doing following:
//from source[source] to multiple targets[targ1,targ2]
System.Text.Encoding targ1 = Encoding.GetEncoding("Target_1_code_name");
System.Text.Encoding targ2 = Encoding.GetEncoding("Target_2_code_name");
System.Text.Encoding source = Encoding.GetEncoding("Source_code_name");
byte[] sourceBytes = source.GetBytes(source);
byte[] targ1Bytes = Encoding.Convert(source, targ1, sourceBytes, 0,
sourceBytes.Length);
byte[] targ2Bytes = Encoding.Convert(source, targ2, sourceBytes, 0,
sourceBytes.Length);
string targ1String = source.GetString(targ1Bytes,0,targ1Bytes.Length);
string targ2String = source.GetString(targ2Bytes,0,targ2Bytes.Length);

BUT Nothing happens....(the source strings continue to be looking like it
was, target undepended). e.g.targ1String = targ2String =source;

What can be a problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #4

P: n/a
Following ALL options tried - Nothing works
HOW TO DI IT?
------------------------------------BIG
CHUCK---------------------------------
static string ConvertFormat(string source,int sourceFormat, int
targetFormat)
{
if(source == null)
return source;

System.Text.Encoding sourceEnc = Encoding.GetEncoding(sourceFormat);
System.Text.Encoding targetEnc = Encoding.GetEncoding(targetFormat);
byte[] sourceBytes = sourceEnc.GetBytes(source);
byte[] targetBytes = Encoding.Convert(sourceEnc,targetEnc,sourceBytes);

char[] targetChars = new char[targetEnc.GetCharCount(targetBytes)];
targetEnc.GetChars(targetBytes, 0, targetBytes.Length, targetChars, 0);
return new string(targetChars);
//return sourceEnc.GetString(targetBytes);
}

static string ConvertEncoding(string source, int sourceFormat, int
targetFormat)
{
if(source==null)
return source;
System.Text.Encoding sourceEnc = Encoding.GetEncoding(sourceFormat);
System.Text.Encoding targetEnc = Encoding.GetEncoding(targetFormat);
byte[] sourceBytes = sourceEnc.GetBytes(source);
return targetEnc.GetString(sourceBytes);
}

public static string ConvertEncode(string source, int targetFormat)
{
if(source==null)
return source;

System.Text.Encoding targetEnc = Encoding.GetEncoding(targetFormat);
byte[] bytes = targetEnc.GetBytes(source);
char[] chars = new char[bytes.Length];
for(int index=0; index<bytes.Length; index++)
{
chars[index] = Convert.ToChar(bytes[index]);
}

string s = new string(chars);
return s;
}

------------------------------------END
CHUNK--------------------------------

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
I have Windows Form application recieved data from clipboard and convert
its
encoding based on some ruls.


If you've received the data as a string, you don't need to do any
encoding conversion. It's already in Unicode.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5

P: n/a
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Following ALL options tried - Nothing works
HOW TO DI IT?


It would help if you'd say exactly what you were trying to do.

The string is already in Unicode - you shouldn't be trying to convert
it to anything else, as strings are *always* in Unicode in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
I have to convert from different formats to different formats
Just for example: from
20866 to

1251

Following the source string:

интеллигентка
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Following ALL options tried - Nothing works
HOW TO DI IT?


It would help if you'd say exactly what you were trying to do.

The string is already in Unicode - you shouldn't be trying to convert
it to anything else, as strings are *always* in Unicode in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

P: n/a
But I need conversion from one code page to another, does it means it's
impossible?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:ey**************@TK2MSFTNGP10.phx.gbl...
Hi,

Strings (I mean System.String) are always Unicode - hence targ1String ==
targ2String == source.
But if you compare sourceBytes, targ1Bytes and targ2Bytes, these should be
different.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:Ov**************@TK2MSFTNGP10.phx.gbl...
I have Windows Form application recieved data from clipboard and convert
its
encoding based on some ruls. So doing following:
//from source[source] to multiple targets[targ1,targ2]
System.Text.Encoding targ1 = Encoding.GetEncoding("Target_1_code_name");
System.Text.Encoding targ2 = Encoding.GetEncoding("Target_2_code_name");
System.Text.Encoding source = Encoding.GetEncoding("Source_code_name");
byte[] sourceBytes = source.GetBytes(source);
byte[] targ1Bytes = Encoding.Convert(source, targ1, sourceBytes, 0,
sourceBytes.Length);
byte[] targ2Bytes = Encoding.Convert(source, targ2, sourceBytes, 0,
sourceBytes.Length);
string targ1String = source.GetString(targ1Bytes,0,targ1Bytes.Length);
string targ2String = source.GetString(targ2Bytes,0,targ2Bytes.Length);

BUT Nothing happens....(the source strings continue to be looking like it
was, target undepended). e.g.targ1String = targ2String =source;

What can be a problem
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nov 16 '05 #8

P: n/a
Wow, the source string looks like it is in Russian :-)
And it looks like you need to convert it from CP 866 (DOS Cyrillic) to Win
1251 (Win Cyrillic). Well, let me explain. The targ1Bytes and targ2Bytes
*ARE* the results of the conversion - they contain the actual bytes
representing the string in the target encoding. For example, you can write
these bytes to a file and you'll have the file with the string in the target
encoding.

So the question is - what you are going to do with the results?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
I have to convert from different formats to different formats
Just for example: from
20866 to

1251

Following the source string:

интеллигентка
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Following ALL options tried - Nothing works
HOW TO DI IT?


It would help if you'd say exactly what you were trying to do.

The string is already in Unicode - you shouldn't be trying to convert
it to anything else, as strings are *always* in Unicode in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #9

P: n/a
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
I have to convert from different formats to different formats
Just for example: from
20866 to

1251

Following the source string:

интеллигентка


But a string effectively doesn't *have* an encoding - it's just in
Unicode. While you have the data in a string, you don't need to worry
about an encoding. You only need to worry about an encoding if you need
to convert from/to a byte array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

P: n/a
I'm russian for this case ;) , but the project should work with more then
600 codepages ;)
Anyway. I'm going to display it in windows form or set to clipboard.
Following source are for sourceEnc - as well textbox and clipboard

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Wow, the source string looks like it is in Russian :-)
And it looks like you need to convert it from CP 866 (DOS Cyrillic) to Win
1251 (Win Cyrillic). Well, let me explain. The targ1Bytes and targ2Bytes
*ARE* the results of the conversion - they contain the actual bytes
representing the string in the target encoding. For example, you can write
these bytes to a file and you'll have the file with the string in the
target encoding.

So the question is - what you are going to do with the results?

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
I have to convert from different formats to different formats
Just for example: from
20866 to

1251

Following the source string:

интеллигентка
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Following ALL options tried - Nothing works
HOW TO DI IT?

It would help if you'd say exactly what you were trying to do.

The string is already in Unicode - you shouldn't be trying to convert
it to anything else, as strings are *always* in Unicode in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #11

P: n/a
Let me try to explain:
I have some text in textbox/clipboard in some encoding (codepage) and should
display it in other textbox or replace clipoard buffer with the same string,
but in other encoding

So I have to convert to byte from from byte to destinatiopn string

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
I have to convert from different formats to different formats
Just for example: from
20866 to

1251

Following the source string:

интеллигентка


But a string effectively doesn't *have* an encoding - it's just in
Unicode. While you have the data in a string, you don't need to worry
about an encoding. You only need to worry about an encoding if you need
to convert from/to a byte array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12

P: n/a
Tamir Khason <ta**********@tcon-NOSPAM.co.il> wrote:
Let me try to explain:
I have some text in textbox/clipboard in some encoding (codepage) and should
display it in other textbox or replace clipoard buffer with the same string,
but in other encoding

So I have to convert to byte from from byte to destinatiopn string


No, you really don't. .NET does all the conversion for you - if the
text has been correctly placed on the clipboard, you should just be
able to get it as a string and display it. There's no such concept as
"the same string but in other encoding".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.