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

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

问题描述

我有一个字符串对象

与多个字符,甚至特殊字符

我试图用

  UTF8Encoding UTF8 =新UTF8Encoding();
ASCIIEncoding ASCII =新ASCIIEncoding();

以该字符串转换为ASCII对象。请问别人带来一些光这个简单的任务,那就是我的狩猎下午。

编辑1:
我们试图完成什么除暴安良的特殊字符,如某些特殊的窗户撇号。在code,我贴在下面作为一个答案将不采取照顾。基本上


  

奥布赖恩将成为Ø?布赖恩。其中是特殊撇号之一



解决方案

这是为了回应你的另一个问题,那看起来像是被删除....问题依然存在。

看起来像一个经典的Uni code到ASCII问题。诀窍是找到的其中的它的发生。

.NET正常工作与统一code,假设它告诉它的统一$ C $ç开始与(或保留在默认的)。

我的的是,你的接收应用程序无法处理它。所以,我可能会使用 ASCIIEn codeR 的<一个href=\"http://msdn.microsoft.com/en-us/library/system.text.en$c$crreplacementfallback.aspx\">En$c$crReplacementFallback用的String.Empty:

 使用System.Text;字符串inputString = GetInput();
变种EN codeR = ASCIIEncoding.GetEn codeR();
EN coder.Fallback =新恩coderReplacementFallback(的String.Empty);字节[] bAsciiString = EN coder.GetBytes(inputString);//使用字节的东西...
//可以写入一个文件是
File.WriteAllBytes(FILE_NAME,bAsciiString);
//或变回一个干净的字符串
字符串cleanString = ASCIIEncoding.GetString(bAsciiString);
//因为违规字节已被删除,可以使用默认的编码以及
Assert.AreEqual(cleanString,Default.GetString(bAsciiString));

当然,在过去,我们刚刚循环,但并删除大于127 ......嗯,那些任何字符我们至少在美国。 ;)

I have a string object

"with multiple characters and even special characters"

I am trying to use

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

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.

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 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.

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

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

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));

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在C#中ASCII(单字节)转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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