GameServer
EndianConverter.h
[詳解]
1 
6 #ifndef __ENDIANCONVERTER_H__
7 #define __ENDIANCONVERTER_H__
8 
14 {
15 
16 public:
17 
23  static bool IsLittleEndian()
24  {
25  int x = 1;
26  char ch = (*(char *)&x);
27  return (ch != 0);
28  }
29 
37  static s16 Convert(s16 Value, bool bForce = false)
38  {
39  if (IsLittleEndian() || bForce)
40  {
41  Value = (((Value << 8) & 0xFF00) | ((Value >> 8) & 0x00FF));
42  }
43  return Value;
44 
45  }
46 
54  static u16 Convert(u16 Value, bool bForce = false)
55  {
56  if (IsLittleEndian() || bForce)
57  {
58  Value = (((Value << 8) & 0xFF00) | ((Value >> 8) & 0x00FF));
59  }
60  return Value;
61 
62  }
63 
71  static s32 Convert(s32 Value, bool bForce = false)
72  {
73  if (IsLittleEndian() || bForce)
74  {
75  s16 Low, High;
76  High = (Value & 0xFFFF0000) >> 16;
77  Low = Value & 0x0000FFFF;
78  Value = (Convert(Low, bForce) << 16) | Convert(High, bForce);
79  }
80  return Value;
81 
82  }
83 
91  static u32 Convert(u32 Value, bool bForce = false)
92  {
93  if (IsLittleEndian() || bForce)
94  {
95  u16 Low, High;
96  High = (Value & 0xFFFF0000) >> 16;
97  Low = Value & 0x0000FFFF;
98  Value = (Convert(Low, bForce) << 16) | Convert(High, bForce);
99  }
100  return Value;
101  }
102 
110  static float Convert(float Value, bool bForce = false)
111  {
112  union FloatConv
113  {
114  u32 u;
115  float f;
116  };
117 
118  FloatConv Conv;
119 
120  Conv.f = Value;
121  Conv.u = Convert(Conv.u, bForce);
122 
123  return Conv.f;
124  }
125 
126 private:
127 
128  //暗黙で定義されるものの封印
129  EndianConverter();
130  EndianConverter(const EndianConverter &Arg);
131  EndianConverter operator =(const EndianConverter &Arg);
132 
133 };
134 
135 #endif //#ifndef __ENDIANCONVERTER_H__
static s32 Convert(s32 Value, bool bForce=false)
符号付き32ビット変換
Definition: EndianConverter.h:71
static bool IsLittleEndian()
リトルエンディアン?
Definition: EndianConverter.h:23
int s32
Definition: TypeDefs.h:9
static u32 Convert(u32 Value, bool bForce=false)
符号なし32ビット変換
Definition: EndianConverter.h:91
unsigned int u32
Definition: TypeDefs.h:10
static u16 Convert(u16 Value, bool bForce=false)
符号なし16ビット変換
Definition: EndianConverter.h:54
unsigned short u16
Definition: TypeDefs.h:8
static s16 Convert(s16 Value, bool bForce=false)
符号付き16ビット変換
Definition: EndianConverter.h:37
エンディアン変換
Definition: EndianConverter.h:13
static float Convert(float Value, bool bForce=false)
float変換
Definition: EndianConverter.h:110
short s16
Definition: TypeDefs.h:7