如何转换(音译)字符串从utf8到ASCII(单字节)在c#? [英] How to convert (transliterate) a string from utf8 to ASCII (single byte) in c#?

查看:98
本文介绍了如何转换(音译)字符串从utf8到ASCII(单字节)在c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串对象

具有多个字符甚至特殊字符

"with multiple characters and even special characters"

我正在尝试使用

UTF8Encoding utf8 = new UTF8Encoding();
ASCIIEncoding ascii = new ASCIIEncoding();

对象,以将该字符串转换为ascii。请问有人可以点亮这个简单的任务,那就是狩猎我的下午。

objects in order to convert that string to ascii. May I ask someone to bring some light to this simple task, that is hunting my afternoon.

编辑1:
我们要完成的是摆脱的特殊字符,如一些特殊的窗口撇号。我在下面发布的代码作为答案不会照顾。基本上

EDIT 1: What we are trying to accomplish is getting rid of special characters like some of the special windows apostrophes. The code that I posted below as an answer will not take care of that. Basically


O'Brian将成为O?Brian。其中'是特殊撇号之一

O'Brian will become O?Brian. where ' is one of the special apostrophes


推荐答案

这是为了回应您的其他问题,看起来它已经被删除了....这一点依然存在。

This was in response to your other question, that looks like it's been deleted....the point still stands.

看起来像一个经典的Unicode到ASCII问题。诀窍将是找到发生的

Looks like a classic Unicode to ASCII issue. The trick would be to find where it's happening.

.NET可以正常使用Unicode,假设它被告知它是Unicode 开头(或默认为左)。

.NET works fine with Unicode, assuming it's told it's Unicode to begin with (or left at the default).

我的猜测是你的接收应用程序无法处理。所以,我可能会使用 ASCIIEncoder 一个 EncoderReplacementFallback with String.Empty:

My guess is that your receiving app can't handle it. So, I'd probably use the ASCIIEncoder with an EncoderReplacementFallback with String.Empty:

using System.Text;

string inputString = GetInput();
var encoder = ASCIIEncoding.GetEncoder();
encoder.Fallback = new EncoderReplacementFallback(string.Empty);

byte[] bAsciiString = encoder.GetBytes(inputString);

// Do something with bytes...
// can write to a file as is
File.WriteAllBytes(FILE_NAME, bAsciiString);
// or turn back into a "clean" string
string cleanString = ASCIIEncoding.GetString(bAsciiString); 
// since the offending bytes have been removed, can use default encoding as well
Assert.AreEqual(cleanString, Default.GetString(bAsciiString));

当然,在过去,我们只是循环,删除任何字符大于127 ...好吧,我们至少在美国。 ;)

Of course, in the old days, we'd just loop though and remove any chars greater than 127...well, those of us in the US at least. ;)

这篇关于如何转换(音译)字符串从utf8到ASCII(单字节)在c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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