๋ฐ์ดํธ ์์(Byte Order)๋ ์ค๋ ์ ์ปดํจํฐ๊ณตํ๊ณผ์์ ๋ฐฐ์ ๋ ๊ฐ๋ ์ด์ง๋ง ์ค๋ฌด์์๋ ์ค์ ๋ก ๊ณ ๋ คํ ํ์๊ฐ ์์๋ค.
์ปดํจํฐ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํ ๋์ ๋ฐ์ดํธ ์์๋ ๋น ์๋์๊ณผ ๋ฆฌํ ์๋๋ฐฉ ์์ผ๋ก ๋๋์ด์ง๋๋ฐ ์ด๊ฒ์ CPU ์ํคํ ์ฒ์ ๋ฐ๋ผ ๋๋์ด์ง๋ค๊ณ ํ๋ค. TCP ๋คํธ์ํฌ ์คํ ์์๋ ํจํท์ ๋ฐ์ดํธ ์์๋ ๋น ์๋์์ผ๋ก ๊ตฌ์ฑ๋๋ฉฐ ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ์์ฃผ ํ์ฉ๋๋ ์ธ์ด์ธ ์๋ฐ๋ ์ด๋ฅผ ๊ฐ์ํ์ฌ ๋น ์๋์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉํ๋ค. CPU ์ํคํ ์ฒ์ ๋ค๋ฅด๊ฒ ๋น ์๋์์ผ๋ก ์ฌ์ฉ๋๋ ์ด์ ๋ JVM ์ด๋ผ๋ ๋ณ๋์ ์คํ์ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ๋๋๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ํ๋ค. ๊ทธ๋ฌ๋, SCADA ๋๋ PLC ์ฅ๋น์ ๊ฐ์ ์ผ๋ถ ์์คํ ์์๋ ๋น ์๋์ ๋ฐฉ์์ด ์๋ ๋ฆฌํ ์๋์ ๋๋ ๋ฏธ๋ ๋ฆฌํ ์๋์์ผ๋ก ๋ฐ์ดํธ ์์๋ฅผ ๊ตฌ์ฑํ๊ณ ์๋๋ฐ ์ด๋ฌํ ์์คํ ์ C ์ธ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ง๊ธฐ ๋๋ฌธ์ด๋ค.
์ผ๋ฐ์ ์ธ ์๋ฐ ์น ๊ฐ๋ฐ์๋ค์ด ๋ฐ์ดํธ ์์๋ฅผ ๊ณ ๋ คํ์ฌ ๋นํธ ์ฐ์ฐ ๋๋ ์ํํธ ์ฐ์ฐ์ ์ํํ ๊ฒฝ์ฐ๋ ๋ง์ง ์์ ๊ฒ ๊ฐ๋ค.
Mid Little Endian, MLE โ
๋น
์๋์์ ๋ฆฌํ ์๋์ ๋ฟ๋ง ์๋๋ผ ๋ฐ์ดํธ ์์๋ฅผ ์ค์ํ๋ ๋ฏธ๋ ๋ฆฌํ ์๋์์ AB CD
์ ๋ฐ์ดํธ ์์๊ฐ ์์๋ CD AB
๋ก ํํํ๋ ๊ฒ์ ์๋ฏธํ๋ค. 32๋นํธ๋ก ๊ตฌ์ฑ๋๋ Float ํ์์ ๋ฏธ๋ ๋ฆฌํ ์๋์ ๋ฐฉ์์ผ๋ก ๊ตฌ์ฑํ ๊ฒ์ Float32MLE
์ด๋ผ๊ณ ํ๊ธฐํ ์ ์๋ค.
Base Convert: IEEE 754 Floating Point
Float32MLE Example โ
@DisplayName("Endian Test")
class EndianTest {
@DisplayName("Float32 Mid Little Endian")
@Test
void TestFloat32MLE() {
String hexadecimal = "00004260"; // 00 00 42 60
byte[] bytes = HexFormat.of().parseHex(hexadecimal);
float originValue = PooledByteBufAllocator.DEFAULT.buffer()
.writeBytes(new byte[]{bytes[2], bytes[3]}) // CD (42 60)
.writeBytes(new byte[]{bytes[0], bytes[1]}) // AB (00 00)
.readFloat();
float value = BigDecimal.valueOf(originValue)
.setScale(1, RoundingMode.HALF_UP)
.floatValue();
Assertions.assertEquals(56.0f, value);
}
}
Netty์ ByteBuf ํด๋์ค์์๋ ๋น
์๋์ ๋ฐฉ์์ readFloat
ํจ์์ ๋ฆฌํ ์๋์ ๋ฐฉ์์ readFloatLE
ํจ์๋ฅผ ์ ๊ณตํ๊ณ ์๋ค. ๊ทธ๋ฌ๋, ๋ฏธ๋ ๋ฆฌํ ์๋์ ๋ฐฉ์์ ๊ฒฝ์ฐ์๋ ๋ณ๋๋ก ์ ๊ณตํ์ง ์์ผ๋ฏ๋ก ์์ ๊ฐ์ด ๋ฒํผ์ CD AB
์์ผ๋ก ์ฐ๊ณ ๋์ ๋น
์๋์ ๋ฐฉ์์ผ๋ก ์ฝ์ด์ผํ๋ค.