In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: Conversion of string binary to hex string(Japanese text)

Get Adobe Flash player
by jkltf 19 Mar 2009
// forked from jkltf's Conversion of string binary to hex string
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    public class StringBinaryConverter extends Sprite {
        private static const SOURCE:String = "人は誰でもみんな死ぬさ";

        public function StringBinaryConverter () {
            // write as3 code here..
            var debug:TextField = new TextField();
            debug.width = debug.height = 600;
            addChild(debug);
            debug.appendText("source string: " + SOURCE + "\n");
            var es:String = encode(SOURCE);
            debug.appendText("encoded string: " + es + "\n");
            var ds:String = decode(es);
            debug.appendText("decoded string: " + ds+ "\n");
        }

        public static function encode(value:String):String {
            var ba:ByteArray = new ByteArray();
            ba.writeUTFBytes(value);
            var len:uint = ba.length;
            var s:String = "";
            for (var i:uint = 0; i < len; i++) {
                s += ba[i].toString(16);
            }
            return s;
        }
        
        public static function decode(value:String):String {
            var ba:ByteArray = new ByteArray();
            var len:uint = value.length;
            for (var i:uint = 0; i < len; i += 2) {
                var c:String = value.charAt(i) + value.charAt(i + 1);
                ba.writeByte(parseInt(c, 16));
            }
            return ba.toString();
        }
    }
}