这是为什么出现在我的C#字符串:£ [英] Why is this appearing in my c# strings: £

查看:152
本文介绍了这是为什么出现在我的C#字符串:£的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在初始化C#中的字符串如下:

I have a a string in c# initialised as follows:

string strVal = "£2000";

然而,每当我写这串出下面写的是:

However whenever I write this string out the following is written:

2000£

它不块钱做到这一点。

code的一个例子位我使用写出来的值:

An example bit of code I am using to write out the value:

System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("/logging.txt"), strVal);

我猜它是与定位,但如果C#中的字符串只是单向code想必这应该只是工作?

I'm guessing it's something to do with localization but if c# strings are just unicode surely this should just work?

澄清:更多的只是一点信息,乔恩斯基特的答案是正确的,但是我也得到了问题,当我URLEn code中的字符串。有$ P $的pventing这种方式?

CLARIFICATION: Just a bit more info, Jon Skeet's answer is correct, however I also get the issue when I URLEncode the string. Is there a way of preventing this?

所以URL连接codeD字符串看起来是这样的:

So the URL encoded string looks like this:

的%C2%a32000

"%c2%a32000"

%C2 = A
%A3 =£

%c2 = Â %a3 = £

如果我带code作为ASCII的£出来的?

If I encode as ASCII the £ comes out as ?

任何更多的想法?

推荐答案

在HTML页面和在HTTP头中使用时的URL的默认字符集被称为的 ISO-8859-1 或ISO Latin-1的。

The default character set of URLs when used in HTML pages and in HTTP headers is called ISO-8859-1 or ISO Latin-1.

这不是同一为UTF-8,和它不一样的ASCII,但它并放入一个字节每字符。 127范围0很像ASCII,整个范围为0至255相同的范围内统一code的0000-00FF。

It's not the same as UTF-8, and it's not the same as ASCII, but it does fit into one-byte-per-character. The range 0 to 127 is a lot like ASCII, and the whole range 0 to 255 is the same as the range 0000-00FF of Unicode.

所以,你可以从C#字符串由每个字符转换为一字节生成它,或者你可以使用 Encoding.GetEncoding(ISO-8859-1)来获取对象做转换为你。

So you can generate it from a C# string by casting each character to a byte, or you can use Encoding.GetEncoding("iso-8859-1") to get an object to do the conversion for you.

(在此字符集,英镑符号是163)。

(In this character set, the UK pound symbol is 163.)

背景

的RFC说的unen codeD文本必须限制传统的7位US ASCII范围,和其他任何(加上特殊的URL分隔符)必须设有codeD。但它留下的什么字符集以用于8位范围的上半部分的问题,使得它依赖于在其中显示的网址的上下文

The RFC says that unencoded text must be limited to the traditional 7-bit US ASCII range, and anything else (plus the special URL delimiter characters) must be encoded. But it leaves open the question of what character set to use for the upper half of the 8-bit range, making it dependent on the context in which the URL appears.

和这方面是由其他两个标准,HTTP和HTML,这确实指定默认的字符集定义,它们共同创建实施者几乎不可抗拒的力量的假设,地址栏包含百分号编码引用ISO- 8859-1。

And that context is defined by two other standards, HTTP and HTML, which do specify the default character set, and which together create a practically irresistable force on implementers to assume that the address bar contains percent-encodings that refer to ISO-8859-1.

ISO-8859-1 是通过HTTP <发送的字符集的基于文本的内容/一>除非另有规定。因此,通过一个URL字符串出现在HTTP GET头的时候,它应该是在ISO-8859-1。

ISO-8859-1 is the character set of text-based content sent via HTTP except where otherwise specified. So by the time a URL string appears in the HTTP GET header, it ought to be in ISO-8859-1.

另外一个因素是,HTML还采用ISO-8859-1作为其默认和URL通常源于在HTML页面的链接。所以,当你手艺在记事本一个简单的最小的HTML页面,键入到该文件的URL是ISO-8859-1。

The other factor is that HTML also uses ISO-8859-1 as its default, and URLs typically originate as links in HTML pages. So when you craft a simple minimal HTML page in Notepad, the URLs you type into that file are in ISO-8859-1.

它有时被描述为标准的洞,但它不是真的;它只是HTML / HTTP的空白由RFC针对URL留下填写。

It's sometimes described as "hole" in the standards, but it's not really; it's just that HTML/HTTP fill in the blank left by the RFC for URLs.

因此​​,例如,在此页面的建议

Hence, for example, the advice on this page:

字符的URL编码包括
  一个%符号,接着的
  两位十六进制再presentation
  了ISO-拉丁(不区分大小写)
  code点的字符。

URL encoding of a character consists of a "%" symbol, followed by the two-digit hexadecimal representation (case-insensitive) of the ISO-Latin code point for the character.

(ISO拉丁是IS-8859的另一个名字)。

(ISO-Latin is another name for IS-8859-1).

这么多的理论。粘贴到记事本,将其保存为一个.html文件,并在一些浏览器中打开它。点击链接和谷歌应搜索英镑。

So much for the theory. Paste this into notepad, save it as an .html file, and open it in a few browsers. Click the link and Google should search for UK pound.

<HTML>
  <BODY>
    <A href="http://www.google.com/search?q=%a3">Test</A>
  </BODY>
</HTML>

它工作在IE,火狐,Safari浏览器的苹果,谷歌Chrome - 我没有任何可用的人,现在

It works in IE, Firefox, Apple Safari, Google Chrome - I don't have any others available right now.

这篇关于这是为什么出现在我的C#字符串:£的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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