AMF各数据类型的解释


前面列出了每种数据类型的表示方法,这样看并不容易理解,下面我就主要讲解一下常用的一些格式:
0.Number这里指的是double类型,数据用8字节表示,比如十六进制00 40 10 00 00 00 00 00 00就表示的是一个double数4.0,在C#中可以使用如下代码读取该数据:

byte[] d=new byte[]{0,0,0,0,0,0,0×10,0×40};//这里的顺序是和amf文件中的顺序正好相反,不要忘记了
double num=BitConverter.ToDouble(d,0);

1.Boolean对应的是.net中的bool类型,数据使用1字节表示,和C语言差不多,使用00表示false,使用01表示true。比如十六进制01 01就表示true。

2.String相当于.net中的string类型,String所占用的空间有1个类型标识字节和2个表示字符串UTF8长度的字节加上字符串UTF8格式的内容组成。比如十六进制03 00 08 73 68 61 6E 67 67 75 61表示的就是字符串,该字符串长8字节,字符串内容为73 68 61 6E 67 67 75 61,对应的就是“shanggua”。在C#中要读取字符串则使用:

byte[] buffer=new byte[]{0x73,0x68,0x61,0x6E,0x67,0x67,0x75,0x61};//03 00 08 73 68 61 6E 67 67 75 61
string str=System.Text.Encoding.UTF8.GetString(buffer);

3.Object在.net中对应的就是Hashtable,内容由UTF8字符串作为Key,其他AMF类型作为Value,该对象由3个字节:00 00 09来表示结束。C#中读取该对象使用如下方法:

private Hashtable ReadUntypedObject()
{
Hashtable hash = new Hashtable();
string key = ReadShortString();
for (byte type = ReadByte(); type != 9; type = ReadByte())
{
hash.Add(key, ReadData(type));
key = ReadShortString();
}
return hash;
}

5.Null就是空对象,该对象只占用一个字节,那就是Null对象标识0x05。

6. Undefined 也是只占用一个字节0x06。

8.MixedArray相当于Hashtable,与3不同的是该对象定义了Hashtable的大小。读取该对象的C#代码是:

private Hashtable ReadDictionary()
{
int size = ReadInt32();
Hashtable hash = new Hashtable(size);
string key = ReadShortString();
for (byte type = ReadByte(); type != 9; type = ReadByte())
{
object value = ReadData(type);
hash.Add(key, value);
key = ReadShortString();
}
return hash;
}

10.Array对应的就是.net中的ArrayList对象,该对象首先使用32位整数定义了ArralyList的长度,然后是密集的跟着ArrayList中的对象,读取该对象使用如下函数:

private ArrayList ReadArray()
{
int size = ReadInt32();
ArrayList arr = new ArrayList(size);
for (int i = 0; i < size; ++i)
{
arr.Add(ReadData(ReadByte()));
}
return arr;
}

11.Date对应.net中的DateTime数据类型,Date在类型标识符0x0B后使用double来表示从1970/1/1到表示的时间所经过的毫秒数,然后再跟一个ushort的16位无符号整数表示时区。读取Date类型的C#代码为:

private DateTime ReadDate()
{
double ms = ReadDouble();
DateTime BaseDate = new DateTime(1970, 1, 1);
DateTime date = BaseDate.AddMilliseconds(ms);
ReadUInt16(); //get’s the timezone
return date;
}

12.LongString对应的也是string类型,不过和2对应的String不同的是这里使用32位整数来表示字符串的UTF8长度,而String使用的是16位。

15.XML是使用类型标识符0x0F后直接跟LongString类型的字符串表示。


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

验证码 * Time limit is exhausted. Please reload CAPTCHA.