ÀÚ¹Ù ±âº» À¯ÇüÀÇ ¹ÙÀÌÆ® ¼ø¼ º¯È¯
author : Yoon Kyung Koo(yoonforh@yahoo.com)
Copyright (c) 1999-2002 Yoon Kyung Koo, All rights reserved.
LITTLE ENDIANÀº LSB°¡ óÀ½¿¡ ³ª¿À°í
BIG ENDIANÀº MSB°¡ óÀ½¿¡ ³ª¿É´Ï´Ù.
±×¸®°í ³×Æ®¿÷ ¹ÙÀÌÆ® ¼ø¼´Â BIG ENDIANÀÔ´Ï´Ù.
¸¸¾à 4¹ÙÀÌÆ® (Áï, ÀÚ¹ÙÀÇ int, 32ºñÆ® ±â°è¿¡¼ CÀÇ int³ª long)¶ó¸é
ÀÚ¹Ù³ª Big Endian¿¡¼ 1-2-3-4 ¼ø¼·Î ¹ÙÀÌÆ®°¡ ¹èÄ¡µÇ¸é
Little EndianÀº 4-3-2-1 ¼ø¼·Î ¹ÙÀÌÆ®¸¦ Çؼ®ÇØ¾ß ÇÕ´Ï´Ù.
Áï, int typeÀÎ ¼ýÀÚ 1Àº little endian¿¡¼´Â (1, 0, 0, 0)·Î Ç¥½ÃµÇ¸ç
big endian¿¡¼´Â (0, 0, 0, 1)·Î Ç¥½ÃµË´Ï´Ù.
float³ª doubleÀÇ °æ¿ì´Â ½Ã½ºÅÛ¿¡ µû¶ó Á»´õ º¹ÀâÇÕ´Ï´Ù.
ÇÏÁö¸¸ IEEE 754¸¦ µû¸£´Â °æ¿ì¿¡´Â ´Ü¼øÈ÷ ¹ÙÀÌÆ® ¼ø¼¸¸ ¹Ù²Ù¸é Çؼ® °¡´ÉÇÕ´Ï´Ù.
C¿¡¼
floatÀº º¸Åë 32ºñÆ®(4¹ÙÀÌÆ®)ÀÌ°í
doubleÀº º¸Åë 64ºñÆ®(8¹ÙÀÌÆ®)ÀÔ´Ï´Ù.
ÀÚ¹Ùµµ ÀÌ¿¡ ÁØÇÒ °ÍÀÔ´Ï´Ù.
µû¶ó¼ ºò ¿£µð¾È¿¡¼ ´õºíÀ» Ç¥ÇöÇÒ ¶§
1-2-3-4-5-6-7-8 ¹ÙÀÌÆ® ¼ø¼¶ó¸é
¸®Æ² ¿£µð¾È¿¡¼´Â
8-7-6-5-4-3-2-1 ¹ÙÀÌÆ® ¼ø¼·Î Çؼ®ÇÏ¸é µË´Ï´Ù.
Áï, long typeÀÎ ¼ýÀÚ 1, Áï 1LÀº little endian¿¡¼´Â (1, 0, 0, 0, 0, 0,
0, 0)·Î Ç¥½ÃµÇ¸ç big endian¿¡¼´Â (0, 0, 0, 0, 0, 0, 0, 1)·Î Ç¥½ÃµË´Ï´Ù.
½ÇÁ¦ ¹ÙÀÌÆ® ¹è¿·Î ³Ñ¾î¿Â °ªÀ» doubleÀ̳ª float·Î º¯È¯ÇÏ´Â µ¥¿¡´Â (ȤÀº ±× ¹Ý´ëÀÇ °æ¿ì¿¡´Â)
´ÙÀ½ ¸Þ¼ÒµåµéÀ» »ç¿ëÇÏ¸é µË´Ï´Ù.
static int Float.floatToIntBits(float);
static int Float.floatToRawIntBits(float);
static float Float.intBitsToFloat(int);
static long Double.doubleToLongBits(double);
static long Double.doubleToRawLongBits(double);
static double Double.longBitsToDouble(long);
´ÙÀ½Àº ÀÚ¹Ù int¸¦ ´Ù¸¥ ¿£µð¾È °ªÀ¸·Î ¹Ù²Ù´Â ¿ø¸®ÀÔ´Ï´Ù.
// Htonl.java
class Htonl {
public static void main(String args[]) {
if (args.length<1) {
System.out.println("Usage : java Htonl ");
System.exit(0);
}
int value=Integer.parseInt(args[0]);
// all java integer is BIG-ENDIAN(network byte-order)
byte[] bytes=new byte[4];
bytes[0]=(byte)((value&0xFF000000)>>24);
bytes[1]=(byte)((value&0x00FF0000)>>16);
bytes[2]=(byte)((value&0x0000FF00)>>8);
bytes[3]=(byte) (value&0x000000FF);
int newValue = 0;
newValue |= (((int)bytes[3])<<24)&0xFF000000;
newValue |= (((int)bytes[2])<<16)&0xFF0000;
newValue |= (((int)bytes[1])<<8)&0xFF00;
newValue |= (((int)bytes[0]))&0xFF;
System.out.println("big endian value = 0x" + Integer.toHexString(value)
+ " ("+bytes[0]+","+bytes[1]+","+bytes[2]+","+bytes[3]+")");
System.out.println("little endian value = 0x" + Integer.toHexString(newValue)
+ " ("+bytes[3]+","+bytes[2]+","+bytes[1]+","+bytes[0]+")");
}
}
¿ø¸®ÀûÀ¸·Î´Â À§¿Í °°Áö¸¸ ½ÇÁ¦ byte order¸¦ º¯°æÇÒ ¶§¿¡´Â ´ÙÀ½°ú °°ÀÌ °£ÆíÇÏ°Ô ÇÒ ¼ö ÀÖ½À´Ï´Ù.
// Htonl2.java
class Htonl2 {
public static void main(String args[]) {
if (args.length<1) {
System.out.println("Usage : java Htonl2 ");
System.exit(0);
}
int value=Integer.parseInt(args[0]);
int newValue = swap(value);
System.out.println("big endian value = 0x" + Integer.toHexString(value)
+ ", little endian value = 0x" + Integer.toHexString(newValue));
}
/*
* Swapping byte orders of given numeric types
*/
static short swap(short x) {
return (short)((x << 8) |
((x >> 8) & 0xff));
}
static char swap(char x) {
return (char)((x << 8) |
((x >> 8) & 0xff));
}
static int swap(int x) {
return (int)((swap((short)x) << 16) |
(swap((short)(x >> 16)) & 0xffff));
}
static long swap(long x) {
return (long)(((long)swap((int)(x)) << 32) |
((long)swap((int)(x >> 32)) & 0xffffffffL));
}
static float swap(float x) {
return Float.intBitsToFloat(swap(Float.floatToRawIntBits(x)));
}
static double swap(double x) {
return Double.longBitsToDouble(swap(Double.doubleToRawLongBits(x)));
}
}
ÀÌ ÆäÀÌÁö´Â 1999³â 9¿ù 10ÀÏ¿¡ óÀ½ ¸¸µé¾îÁ³½À´Ï´Ù.
Æ©Å丮¾ó ÆäÀÌÁö·Î µ¹¾Æ°¡±â
Last modified: Fri Jul 05 13:18:31 +0900 2002