这是代码。
下面的代码似乎不能正常工作:
[Serializable]
public class HostedGame
{
public int ID { get; set; }
public int UID { get; set; }
public String Name { get; set; }
public Boolean Available { get; set; }
public String Description { get; set; }
public List<int> Users { get; set; }
public int Port { get; set; }
public HostedGame(int uid, String name, String description, int port)
{
UID = uid;
Name = name;
Description = description;
Available = true;
Port = port;
Users = new List<int>();
}
public int CompareTo(Object obj)
{
int result = 1;
if(obj != null && obj is HostedGame)
{
HostedGame w = obj as HostedGame;
result = this.ID.CompareTo(w.ID);
}
return result;
}
static public int Compare(HostedGame x, HostedGame y)
{
int result = 1;
if(x != null && y != null)
{
result = x.CompareTo(y);
}
return result;
}
public static HostedGame DeSerialize(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
BinaryFormatter bff = new BinaryFormatter();
return (HostedGame)bff.Deserialize(ms);
}
public static byte[] Serialize(HostedGame obj)
{
BinaryFormatter bff = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bff.Serialize(ms, obj);
return ms.ToArray();
}
}
下面的代码似乎不能正常工作:
HostedGame hs = new HostedGame(12,"Name", "Description", 8088);
String s = Encoding.ASCII.GetString(HostedGame.Serialize(hs));
HostedGame HACK = HostedGame.DeSerialize(Encoding.ASCII.GetBytes(s));
HACK.Port
出现了7999的情况?
当我只是这样做时...
HostedGame HACK = HostedGame.DeSerialize(HostedGame.Serialize(hs));
它运行良好。
所以,我的问题是
- 为什么我得到了错误的值?
- 是否有更好的方法将字节转换为字符串并再次转换回来?