如何在C#中将unicode字符转换为其等效的转义ascii [英] How to convert unicode character to its escaped ascii equivalent in c#

查看:117
本文介绍了如何在C#中将unicode字符转换为其等效的转义ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从包含编码的Unicode字符"& #xfc; "的字符串开始.我将字符串传递给执行某种逻辑并返回另一个字符串的对象.该字符串正在将原始编码的字符转换为等效的unicode"ü".

i am beginning with a string containing an encoded unicode character "& #xfc;". I pass the string to an object that performs some logic and returns another string. That string is converting the original encoded character to its unicode equivalent "ü".

我需要找回原来的编码字符,但到目前为止还不能.

I need to get the original encoded character back but so far am not able.

我尝试使用HttpUtility.HtmlEncode()方法,但是返回的是"ü ",这是不一样的.

I have tried using the HttpUtility.HtmlEncode() method but that is returning "& #252;" which is not the same.

任何人都可以帮忙吗?

推荐答案

它们几乎相同,至少出于显示目的. HttpUtility.HtmlEncode使用十进制编码,而原始版本为十六进制编码,即格式为&#xHEX;.由于十六进制的fc是十进制的252,因此两者是等效的.

They are pretty much the same, at least for display purposes. HttpUtility.HtmlEncode is using decimal encoding, which is in the format &#DECIMAL; while your original version is in hexadecimal encoding, i.e. in the format &#xHEX;. Since fc in hex is 252 in decimal, the two are equivalent.

如果您确实需要获取十六进制编码的版本,请考虑解析十进制和

If you really need to get the hex-encoded version, then consider parsing out the decimal and converting it to hex before stuffing it back in to the &#xHEX; format. Something like

string unicode = "ü";
string decimalEncoded = HttpUtility.HtmlEncode(unicode);
int decimal = int.Parse(decimalEncoded.Substring(2, decimalEncoded.Length - 3);
string hexEncoded = string.Format("&#x{0:X};", decimal);

这篇关于如何在C#中将unicode字符转换为其等效的转义ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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