string 将字符串转换为 ASCII 代码并返回 Flash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4773578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string to ASCII codes and back in Flash
提问by TheFlash
In AS2 you had the functions ord()
and chr()
which specifically converted ASCII codes to their string equivalents.
在AS2你有功能ord()
和chr()
特异性转换ASCII码到它们的字符串的等同物。
However in AS3, String.charCodeAt()
and String.fromCharCode()
work with Unicode values.
但是在 AS3 中,String.charCodeAt()
并String.fromCharCode()
使用 Unicode 值。
Is there any way to convert a string character to its ASCII equivalent and back?
有什么方法可以将字符串字符转换为其等效的 ASCII 字符并返回?
回答by TheFlash
Well I found the answer, and its quite strange.
好吧,我找到了答案,而且很奇怪。
The usual so called "Unicode" functions appear to work with ASCII values as well.
通常所谓的“Unicode”函数似乎也适用于 ASCII 值。
trace(String.fromCharCode(65)) // "A"
trace(("A").charCodeAt(0)) // 65
回答by bob
For the musical notation of sharp and flat use:
对于尖锐和平坦使用的乐谱:
var flat:String = "?";
trace( flat.charCodeAt() ); // output: 9837
trace( String.fromCharCode(9837) ); // output: ?
var sharp:String = "?";
trace( sharp.charCodeAt() ); // output: 9839
trace( String.fromCharCode(9839) ); // output: ?
回答by robertp
weirdly the documentation says these methods work with Unicode but testing them I got the ASCII values. Maybe I miss something, but it looks fine for me.
奇怪的是,文档说这些方法适用于 Unicode,但测试它们我得到了 ASCII 值。也许我错过了一些东西,但对我来说看起来不错。
var str:String = "A";
trace("ASCII dec: " + str.charCodeAt(0));
trace("ASCII hex: " + str.charCodeAt(0).toString(16));
trace("Character: " + String.fromCharCode(str.charCodeAt(0)));
ASCII table: http://www.sciencelobby.com/ascii-table/ascii-table.html
ASCII 表:http: //www.sciencelobby.com/ascii-table/ascii-table.html
Rob
抢