基本转换查询 [英] Basic Conversion Query

查看:58
本文介绍了基本转换查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个字符串(System.String),它包含一些数据。这个数据是UTF8中的
编码(即字符串中应该有一个

单个''é''字符的任何地方,会有两个字符持有

相当于UTF8格式的那个字符)。


如何解码这个UTF8编码的字符串?


在Delphi中我可以简单地说:


myString = UTF8ToAnsi(myString);


如何使用.NET执行此操作?


我尝试使用通用静态方法来执行此操作:


私有静态字符串Utf8ToAscii(字符串值)

{

byte [] utf8Bytes = Encoding.UTF8.GetBytes(value);

byte [] asciiBytes = Encoding.Convert(Encoding.UTF8,

Encoding.Unicode,utf8Bytes);


返回Encoding.Unicode.GetString(asciiBytes);

}


....但它并没有按照预期工作。这只会导致一对

编码的字符被替换为''?''字符。


TIA

Hi,

I have a string (System.String) which holds some data. This data is
encoding in UTF8 (i.e. anywhere in the string where there should be a
single ''é'' character, there will be two characters holding the
equivalent of that character in the UTF8 format).

How can I decode this UTF8-encoded string?

In Delphi I could simple say:

myString = UTF8ToAnsi(myString);

How can I do this using .NET?

I tried making a general-purpose static method to do this:

private static string Utf8ToAscii(string value)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(value);
byte[] asciiBytes = Encoding.Convert(Encoding.UTF8,
Encoding.Unicode, utf8Bytes);

return Encoding.Unicode.GetString(asciiBytes);
}

....but it doesn''t work as desired. This just causes the pair of
encoded characters to be replaced to ''?'' characters.

TIA

推荐答案

查看System.Text.UTF8Encoding类:
http://tinyurl.com/kh15


-

Greetz


Jan Tielens

________________________________

阅读我的博客: http://weblogs.asp.net/jan

" C#Learner" < CS **** @ learner.here>在消息中写道

news:uc ******************************** @ 4ax.com ...
Check out the System.Text.UTF8Encoding class:
http://tinyurl.com/kh15

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"C# Learner" <cs****@learner.here> wrote in message
news:uc********************************@4ax.com...


我有一个字符串(System.String),它包含一些数据。这个数据是以UTF8格式编码的(即字符串中应该有单个''''字符的任何地方,将有两个字符,其中包含相当于该字符的
UTF8格式)。

如何解码这个UTF8编码的字符串?

在Delphi中我可以简单地说:

myString = UTF8ToAnsi(myString );

如何使用.NET实现这一点?

我尝试使用通用的静态方法来执行此操作:

私有静态string Utf8ToAscii(string value)
{
byte [] utf8Bytes = Encoding.UTF8.GetBytes(value);
byte [] asciiBytes = Encoding.Convert(Encoding.UTF8,
Encoding.Unicode,utf8Bytes);

返回Encoding.Unicode.GetString(asciiBytes);
}

...但它没有按预期工作。这只会使一对
编码的字符被替换为''?''字符。

TIA
Hi,

I have a string (System.String) which holds some data. This data is
encoding in UTF8 (i.e. anywhere in the string where there should be a
single ''é'' character, there will be two characters holding the
equivalent of that character in the UTF8 format).

How can I decode this UTF8-encoded string?

In Delphi I could simple say:

myString = UTF8ToAnsi(myString);

How can I do this using .NET?

I tried making a general-purpose static method to do this:

private static string Utf8ToAscii(string value)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(value);
byte[] asciiBytes = Encoding.Convert(Encoding.UTF8,
Encoding.Unicode, utf8Bytes);

return Encoding.Unicode.GetString(asciiBytes);
}

...but it doesn''t work as desired. This just causes the pair of
encoded characters to be replaced to ''?'' characters.

TIA



C#Learner< cs **** @ learner.here>写道:
C# Learner <cs****@learner.here> wrote:
我有一个字符串(System.String),它包含一些数据。这个数据是以UTF8格式编码的(即字符串中应该有单个''''字符的任何地方,将有两个字符,其中包含相当于该字符的
UTF8格式)。

如何解码这个UTF8编码的字符串?
I have a string (System.String) which holds some data. This data is
encoding in UTF8 (i.e. anywhere in the string where there should be a
single ''é'' character, there will be two characters holding the
equivalent of that character in the UTF8 format).

How can I decode this UTF8-encoded string?




请参阅我在microsoft.public.dotnet.framework中的回复。你的基本问题是混合二进制数据和字符数据。


http://www.pobox.com/~skeet/csharp/unicode.html


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Please see my responses in microsoft.public.dotnet.framework. Your
basic problem is mixing up binary data and character data.

See http://www.pobox.com/~skeet/csharp/unicode.html

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


Jon Skeet [C#MVP]< sk *** @ pobox.com>写道:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
C#Learner< cs **** @ learner.here>写道:
C# Learner <cs****@learner.here> wrote:
我有一个字符串(System.String),它包含一些数据。这个数据是以UTF8格式编码的(即字符串中应该有单个''''字符的任何地方,将有两个字符,其中包含相当于该字符的
UTF8格式。

如何解码这个UTF8编码的字符串?
I have a string (System.String) which holds some data. This data is
encoding in UTF8 (i.e. anywhere in the string where there should be a
single ''é'' character, there will be two characters holding the
equivalent of that character in the UTF8 format).

How can I decode this UTF8-encoded string?



请参阅我在microsoft.public.dotnet.framework中的回复。你的基本问题是混合二进制数据和字符数据。

参见 http://www.pobox.com/~skeet/csharp/unicode.html




我真的我看不出我在哪里混淆任何东西。我

只是有一个UTF8格式的字符串。我只想解码它。


在.NET中不可能吗?我已经在Delphi中做到了这一点没有问题。


感谢您的耐心回复。



I really can''t see where I''m mixing up anything with anything. I
simply have a string in UTF8 format. I just want to decode it.

Is it not possible in .NET? I''ve done this in Delphi without problem.

Thanks for your patient replies.


这篇关于基本转换查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆