Hi,
I think I found the correct way to calculate string length for serialising
and deserialising, both the code in the CVS and the code I previously sent
you don't work properly.
In the PHP manual it says Unicode is converted to UTF-8 as follows :
bytes bits representation
1 7 0bbbbbbb
2 11 110bbbbb 10bbbbbb
3 16 1110bbbb 10bbbbbb 10bbbbbb
4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
It seems that Flash only supports Unicode characters up to 16 bits so we
will have to ignore 4 byte UTF-8. We should probably throw an error if
someone tries to send 4 byte but someone else can work on that one.
Here is the code that I have developed and tested with relevant test cases.
private function getCStringLength ():Number {
var colon:Number = this.buffer.indexOf (":", 3); // index of second colon
var ss:Number = colon+2; // index of start of string
var len:Number = parseInt (this.buffer.substr (2, colon - 2));
var i:Number;
var j:Number = len; //char length of string to be calculated
var c:Number;
var cstr = this.buffer;
for (i = ss; i < (ss + j); i++)
{
c=cstr.charCodeAt(i);
if (c<128) {
//do nothing
}else if (c<2048) {
j = j-1;
} else {
j = j-2;
}
}
return j;
}; // getCStringLength
and used to serialize data :
private function calcLength(struct:String)
{
var c;
var result=0;
var l = struct.length;
for (var i=0; i < l; i++)
{
c = (struct.charCodeAt(i));
if(c<128) {
result += 1;
} else if (c<2048) {
result += 2;
} else {
result += 3;
}
}
return result;
}
This seems to be able to serialize and unserialise all unicode characters
supported by Flash properly and construct / reconstruct arrays and strings
which can be unserialised and serialised by php .
Check out the zip file for my test case here :
http://jamiep.org/mbcs/test.zip
and see it in action here :
http://jamiep.org/mbcs/serialtest.html
ALso you may be interested in the way I found to send data to the server :
override XML.toString(). It seems if you make XML.toString() return a
string and then use XML.sendAndLoad() or XML.send() that exactly the
string you return is sent. If you use the XML constructor then XML
converts & to & but not if you override XML.toString(). Old news maybe?
Jamie
|