java

Java > C# UDP 통신 / 리틀엔디안(Little Endian)방식

주피터0410 2023. 8. 21. 16:59
ByteBuffer buffer = ByteBuffer.allocate(8);

// 리틀엔디안(Little Endian) 으로 변환
buffer.order(ByteOrder.LITTLE_ENDIAN);

 

private final static String IP = "192.168.0.8";
private final static int PORT = 9050;
public static void main(String[] args) throws IOException {
InetAddress ia = InetAddress.getByName(IP);
DatagramSocket ds = new DatagramSocket();
// Java에서 둘 이상의 바이트 배열 연결
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(setDoubleToByte(0.0014));
outputStream.write(setDoubleToByte(-0.0027));
byte buffer[] = outputStream.toByteArray();
System.out.println("보낼 length : " + buffer.length);
// 바이트 출력
data(buffer);
// 전송
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, PORT);
ds.send(dp);
ds.close();
}
public static byte[] setDoubleToByte(double number) {
ByteBuffer buffer = ByteBuffer.allocate(8);
// 리틀엔디안(Little Endian) 방식
buffer.order(ByteOrder.LITTLE_ENDIAN);
byte[] bytes = buffer.putDouble(number).array();
return bytes;
}
public static void data(byte[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}