2012年4月30日 星期一

how to get string unicode range

You can use "charCodeAt" to get numeric Unicode character code and covert it to 16 hex.


/*
* @mxmlc -debug -o=UnicodeRange.swf
*/
package
{
import flash.display.Sprite;
/**
* description:get unicode range
* use charCodeAt to get numeric Unicode character code
* and covert it to 16 hex
*/
public class UnicodeRange extends Sprite
{
public function UnicodeRange()
{
getUnicodeRange('123abc我是測試許功蓋怪字');
}
private function getUnicodeRange(__input:String):void
{
var str:String = __input;
var i:uint, n:uint = str.length;
var arr:Array = [];
for (i = 0; i < n; i++)
{
var r:Number = str.charCodeAt(i);
arr[i] = "U+" + convert2Hex(r);
}
arr.sort();
//output: unicodeRange = 'U+0031, U+0032, U+0033, U+0061, U+0062, U+0063, U+529f, U+5b57, U+602a, U+6211, U+662f, U+6e2c, U+84cb, U+8a31, U+8a66'
trace("unicodeRange = '" + arr.join(", ") + "'");
}
private function convert2Hex(__r:Number):String
{
var t:String = __r.toString(16);
var j:uint = t.length;
if (j < 4) {
t = ("0000" + t).substr(-4,4);
}
return t;
}
}
}
view raw UnicodeRange.as hosted with ❤ by GitHub

沒有留言:

張貼留言