473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Encoding.GetEncode Problem

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
12 3918
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your...
8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
7
by: Mark | last post by:
Hi... I've been doing a lot of work both creating and consuming web services, and I notice there seems to be a discontinuity between a number of the different cogs in the wheel centering around...
8
by: davisjoseph | last post by:
Hi All, I'm newbie to this XML world. My problem is to identify the encoding type of XML at runtime. What currently I'm doing is checking whether BOM is available in the XML; based on the BOM...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
5
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
3
by: Martin Z | last post by:
Hi, I have an application that involves sending a lot of XML data to various places. The problem is that once in a while, I just want the XML document as a string (for example, sending to a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you▓ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.