Reputation: 1267
I have a DatagramSocket
and want to send char[] charData = { 0xff, 0x04, 0x02, 0xfb}
data via DatagramSocket
using DatagramPacket
.
DatagramPacket is using byte[]
as data. But I should send it like charData
variable.
Any suggestions?
Upvotes: 0
Views: 2512
Reputation: 1267
I solved the case. I simply do "ff0402fb".getbytes
and its working.
Upvotes: 1
Reputation: 13061
you could use ObjectOutputStream and ByteArrayOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(charData);
byte[] toSend = baos.toByteArray();
oos.close();
baos.close();
int port = xxxx;
DatagramPacket p = new DatagramPacket(toSend,toSend.length,InetAddress.getByName("host"),port);
DatagramSocket s = new DatagramSocket();
s.send(p);
Upvotes: 0