如何在c#中使用utf编码? [英] How to use the utf encoding in c# ?

查看:87
本文介绍了如何在c#中使用utf编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序(winform)使用sql localdb,当我插入阿拉伯语单词时,我在名称colomn中出现了这个问题?????? ,我使用这段代码:

I have application (winform) use a sql localdb, the probleme when I insert arabic word i have in the colomn of the name this character "??????" ,I use this code:

var arabic = Encoding.GetEncoding(864);
var bytes = arabic.GetBytes(libelléTextEdit.Text);
libelléTextEdit.Text = arabic.GetString(bytes);



对我不起作用?

如何解决这个问题?


not work for me ?
How to solve this problem ?

推荐答案

.NET框架支持Unicode,您可以在应用程序中轻松使用Unicode的UTF-8,UTF-16或其他Unicode编码(或字符集)。问题是您需要确定您的应用程序是否能够表示字形。如果您的应用程序的字体系列包含阿拉伯语的字形(例如),那么您可以使用它们。否则,它总会显示????对于每个角色,或者它会显示一些其他类似的角色。



它们并不意味着您的应用程序或.NET框架不支持Unicode,而是意味着该字符未映射到代码页以表示它字形。



我写了一篇文章,完整地描述了Unicode,如何映射字符,以及为什么这些???如果字符未正确映射,则显示。此外,提供的解决方案是您应该使用支持角色代码页的字体系列。在大多数情况下(我的建议也是)Segoe UI。



阅读更多关于我的文章:在.NET中读取和写入Unicode数据 [ ^ ]



我希望这篇文章可以帮到你。 :)
Unicode is supported in .NET framework, and you can easily use Unicode's UTF-8, UTF-16 or other encodings (or character sets) of Unicode in your applications. The thing is that you need to be sure whether your application is able to represent the glyphs or not. If you application's font family includes the glyphs (for example) of Arabic then you can use them. Otherwise, it would always show "????" for each character, or it would show some other similar character.

They do not mean that your application or .NET framework doesn't support Unicode, instead it means that the character was not mapped to the code pages to represent it in glyph.

I have written an article that fully describes Unicode, how characters are mapped, and why are these "???" shown if a characters isn't mapped properly. Also, the solution provided there was that you should use such a font family that supports the character's code page. Which in most cases (and my recommendation also) is Segoe UI.

Read more on my article: Reading and writing Unicode data in .NET[^]

I hope the article helps you out. :)


我的大部分答案已经在我对问题和解决方案1的所有评论中。



简短摘要是: 1)不要使用非Unicode的任何东西; 2)甚至不使用UTF,除非你将数据保存在任何文件或流中;这与数据库和ASP.NET无关(页面应该使用UTF-8,但除HTTP-EQUIV之外的所有内容都是自动完成的); 3)仅使用Unicode字符串数据类型,如nvarchar而不是varchar; ADO.NET会自动将这些数据映射到.NET字符串,但是使用varchar,您将丢失数据并获取?而不是阿拉伯字母(以及东部阿拉伯数字和标点符号)。



您正尝试使用Perso-Arabic脚本,默认情况下几乎所有现代系统都支持Unicode;你不需要安装任何东西。



最后:你真的需要了解Unicode的作用。



-SA
Most of my answer is already in all my comments to the question and Solution 1.

Short summary is: 1) Don't use anything non-Unicode; 2) don't even use UTFs, unless you persist the data in any file or stream; which has nothing to do with databases and ASP.NET (the pages should use UTF-8, but everything except HTTP-EQUIV is done automatically); 3) use only Unicode string data types such as nvarchar instead of varchar; ADO.NET will automatically map such data to .NET strings, but with varchar you will loose data and get '?' instead of Arabic letters (and Eastern Arabic numerals and punctuation).

You are trying to use Perso-Arabic script which is supported via Unicode by default by nearly all modern system; you don't need to install anything.

Finally: you really need to understand what Unicode does.

—SA


感谢您提供所有解决方案,我只需在查询中的数据中添加N并成功插入。
Thank you for all your solution ,i just add N inside the data in the query and is inserted successfully.


这篇关于如何在c#中使用utf编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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