Bonjour a tous, j'ai un petit soucis sur la conversion d'un bout de code du c# au java
Donc j'ai :
PacketType.java
public enum PacketType
{
REQUEST((byte) 1),
RESPONSE((byte) 2);
private byte byteValue;
private PacketType(byte byteValue)
{
this.byteValue = byteValue;
}
public byte getPacketType()
{
return byteValue;
}
public void setPacketType(byte byteValue)
{
this.byteValue = byteValue;
}
public static PacketType getPacketTypeById(byte id)
{
for (PacketType type : values())
{
if (type.getPacketType() == id)
{
return type;
}
}
return null;
}
}
PacketDataType.java
public enum PacketDataType
{
PING((byte) 0),
LOGIN((byte) 1),
LOGINFAIL((byte) 2),
LOGOUT((byte) 3),
STATUSCHANGE((byte) 10),
BUDDYADD((byte) 31),
USERNOTFOUND((byte) 32),
AUTHORIZE((byte) 33),
GETBUDDYLIST((byte) 34),
BUDDYDELETE((byte) 35),
SENDMESSAGE((byte) 40);
private byte byteValue;
private PacketDataType(byte byteValue)
{
this.byteValue = byteValue;
}
public byte getPacketDataType()
{
return byteValue;
}
public void setPacketDataType(byte byteValue)
{
this.byteValue = byteValue;
}
public static PacketDataType getPacketDataTypeById(byte id)
{
for (PacketDataType type : values())
{
if (type.getPacketDataType() == id)
{
return type;
}
}
return null;
}
}
et ma fonction ou j ai un probleme :
public PacketDataType Command;
public PacketType Type;
public Packet()
{
Type = PacketType.REQUEST;
Command = PacketDataType.PING;
}
public byte[] toBytes()
{
int packetsize = 4;
if (Arguments != null && Arguments.length() > 0)
{
packetsize += 2 + Arguments.length();
}
byte[] data = new byte[packetsize];
data[0] = 0x6d;
data[1] = 0x70;
data[2] = (byte) Type; <##################################### Ici
data[3] = (byte) Command; <################################## Ici
if (Arguments != null && Arguments.length() > 0)
{
short argsize = (short) Arguments.length();
byte[] sizebytes = BitConverter.toBytes(argsize);
System.arraycopy(sizebytes, 0, data, 4, 2);
byte[] byArgs = null;
try
{
byArgs = Arguments.getBytes("US-ASCII");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
System.arraycopy(byArgs, 0, data, 6, argsize);
}
return data;
}
Donc sur le cast des mes enums j'ai une erreur 'Cannot cast from PacketDataType to byte' sous eclipse
Y'a t'il moyen en java de cast un enum?
en c# oui car le code si dessous fonctionne:
public byte[] ToBytes()
{
var packetsize = 4;
if(!string.IsNullOrEmpty(m_sArguments))
{
packetsize += 2 + m_sArguments.Length;
}
var data = new byte[packetsize];
data[0] = 0x6d;
data[1] = 0x70;
data[2] = (byte) Type;
data[3] = (byte) Command;
if(!string.IsNullOrEmpty(m_sArguments))
{
var argsize = (short) m_sArguments.Length;
var sizebytes = BitConverter.GetBytes(argsize);
Array.Copy(sizebytes, 0, data, 4, 2);
var enc = new ASCIIEncoding();
var byArgs = enc.GetBytes(m_sArguments);
Array.Copy(byArgs, 0, data, 6, argsize);
}
return data;
}
Afficher la suite