简体   繁体   English

在JavaScript中将Unicode转换为ASCII

[英]Convert Unicode To ASCII in JavaScript

In c#, I have ö ASCII character 148 . 在C#中,我有ö字符148 but when I change like 但是当我改变像

 char convstr = (char)148;

It returns . 返回 Its "”" .Its unicode. 它的"”" 。其unicode。

If I want it back ASCII code, I just use Strings.Asc("”") . 如果要返回ASCII码, Strings.Asc("”")使用Strings.Asc("”") It will return to 148. 返回到148。

So How can I get from "”" to 148 in JavaScript? 那么如何在JavaScript中从“”变为148?

I tried Like that=> 我试过那样=>

"”".charCodeAt(0) but it return 8221 . "”".charCodeAt(0)但返回8221 I think its Unicode. 我认为它是Unicode。

And If you don't mind, please explain to me why char convstr = (char)148; 如果您不介意,请向我解释为什么char convstr = (char)148; return also. 还返回 I am also stuck in there. 我也被困在那里。

================================= ================================

148 isn't the value for ö in ASCII (ie the 7-bit US ASCII encoding that goes only up to 127) nor the commonly-referred-to-as-ASCII codepages 1252 (Windows Latin 1) and ISO/IEC 8859-1 . 148既不是ASCII中ö的值(即仅升至127的7位美国ASCII编码),也不是通常称为ASCII的代码页1252(Windows Latin 1)ISO / IEC 8859- 1 1252 has in that location while the ISO codepage has nothing. 1252在该位置带有 ,而ISO代码页则没有。 That value is used for ö only in the old DOS codepages, 437 and 865. 该值仅在旧的 DOS代码页437和865中用于ö

Windows, .NET and C# strings are Unicode natively. Windows,.NET和C#字符串本身就是Unicode。 This pages proves this - StackOverflow is an ASP.NET site. 这些页面证明了这一点-StackOverflow是一个ASP.NET站点。 You can convert data in non-Unicode encodings easily, either through the Encoding class, or by specifying the encoding when loading data from streams with a StreamReader. 您可以通过Encoding类或通过使用StreamReader从流中加载数据时指定编码来轻松地以非Unicode编码转换数据。

For example, this will convert the byte value 148 to ö using the 437 codepage : 例如,这将使用437代码页将字节值148转换为ö

var result=Encoding.GetEncoding(437).GetString(new byte[]{148});
Debug.Assert(result=="ö");

While this returns : 虽然返回

var result=Encoding.GetEncoding(1252).GetString(new byte[]{148});

The StreamReader(string,Encoding) overload and its variants can load data from files using the specified encoding, eg : StreamReader(string,Encoding)重载及其变体可以使用指定的编码从文件中加载数据,例如:

using(var reader=new StreamReader(path,Encoding.GetEncoding(437)))
{
    var line=reader.ReadLine();
    ....
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM