开发者

Generate/copy UTF-8 text from flash

开发者 https://www.devze.com 2023-02-21 17:15 出处:网络
Edit 04/04/11: It turns out that the UTF encoding was just hiding a bigger problem. I initially thought it was the UTF encoding as setting that seemed to solve the search problem, but it was more to


Edit 04/04/11: It turns out that the UTF encoding was just hiding a bigger problem. I initially thought it was the UTF encoding as setting that seemed to solve the search problem, but it was more to do with how mongodb was serialising the search key in the background


I'm using Flash to create a small tool to generate db info for mongodb. It will generate something like:

db.save({"className":"mypackage.MyClass","name":"someName"});

And display that in a TextField so I can just copy and paste it directly into the db.

My problem comes with mongodb being unable to find that info later. After much debugging, the problem boiled down to the t开发者_开发技巧ext not being entered in UTF-8 format. i.e., in order to make it work, I'd have to copy the line of text from Flash, paste it into something like Notepad++, set the encoding to UTF-8, then copy that text into the db.

So my question is this: is there a way to either specify that I want to generate the text as UTF-8 or copy as UTF-8 from the TextField to avoid having to have this extra step in my workflow.

Thanks


Check out How does "cut and paste" affect character encoding and what can go wrong?.

In a comment someone mentions that, at least on Windows, the clipboard contains different versions of the copied text. It seems the problem is in the app that gets the pasted text (your db).

I've just tried pasting from a TextField into Notepad++ (previously changed the doc enconding to utf-8) and the text came out fine. So, it seems the problem is in the app that receives the text. Maybe you could use some escaping ( like \u00F1 for ñ) but I'm not sure whether that works or not, since I haven't used mongodb.


This is just a thought.

Have you tried a byteArray.

        var b1:ByteArray = new ByteArray();
        var b2:ByteArray = new ByteArray();

        var a:String = "className";
        var b:String = "mypackage.MyClass";
        // supported UTF-8 codes are
        // unicode-1-1-utf-8, 
        // unicode-2-0-utf-8, 
        // x-unicode-2-0-utf-8
        b1.writeMultiByte(a, "unicode-1-1-utf-8");

        /**
        Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method,
        but writeUTFBytes() does not prefix the string with a 16-bit length word.
        */
        b2.writeUTFBytes(b); // encodes straight to UTF-8

        // flash says it honors the encoding mark when using toString
        trace(b1.toString(), b2.toString());

supported char sets can be found at: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/charset-codes.html

0

精彩评论

暂无评论...
验证码 换一张
取 消