UnityMol  0.9.6-875
UnityMol viewer / In developement
TypeConvert.cs
Go to the documentation of this file.
1 
66 namespace Convert
67 {
68  using UnityEngine;
69  using System.Collections;
70 
71  public class TypeConvert
72  {
73 
74  public TypeConvert()
75  {
76  }
77 
78  public static byte[] getBytes(short s, bool asc)
79  {
80  byte[] buf = new byte[2];
81  if (asc)
82  {
83  for (int i = buf.Length - 1; i >= 0; i--)
84  {
85  buf[i] = (byte) (s & 0x00ff);
86  s >>= 8;
87  }
88  }
89  else
90  {
91  for (int i = 0; i < buf.Length; i++)
92  {
93 
94  buf[i] = (byte) (s & 0x00ff);
95  s >>= 8;
96  }
97  }
98  return buf;
99  }
100  public static byte[] getBytes(int s, bool asc)
101  {
102  byte[] buf = new byte[4];
103  if (asc)
104  for (int i = buf.Length - 1; i >= 0; i--)
105  {
106  buf[i] = (byte) (s & 0x000000ff);
107  s >>= 8;
108  }
109  else
110  for (int i = 0; i < buf.Length; i++)
111  {
112  buf[i] = (byte) (s & 0x000000ff);
113  s >>= 8;
114  }
115  return buf;
116  }
117 
118  public static byte[] getBytes(long s, bool asc)
119  {
120  byte[] buf = new byte[8];
121  if (asc)
122  for (int i = buf.Length - 1; i >= 0; i--)
123  {
124  buf[i] = (byte) (s & 0x00000000000000ff);
125  s >>= 8;
126  }
127  else
128  for (int i = 0; i < buf.Length; i++)
129  {
130  buf[i] = (byte) (s & 0x00000000000000ff);
131  s >>= 8;
132  }
133  return buf;
134  }
135  public static short getShort(byte[] buf, bool asc) {
136  if (buf == null) {
137  //throw new IllegalArgumentException("byte array is null!");
138  }
139  if (buf.Length > 2) {
140  //throw new IllegalArgumentException("byte array size > 2 !");
141  }
142  short r = 0;
143  if (asc)
144  for (int i = buf.Length - 1; i >= 0; i--) {
145  r <<= 8;
146  r |= (short)(buf[i] & 0x00ff);
147  }
148  else
149  for (int i = 0; i < buf.Length; i++) {
150  r <<= 8;
151  r |= (short)(buf[i] & 0x00ff);
152  }
153  return r;
154  }
155  public static int getInt(byte[] buf, bool asc) {
156  if (buf == null) {
157  // throw new IllegalArgumentException("byte array is null!");
158  }
159  if (buf.Length > 4) {
160  //throw new IllegalArgumentException("byte array size > 4 !");
161  }
162  int r = 0;
163  if (asc)
164  for (int i = buf.Length - 1; i >= 0; i--) {
165  r <<= 8;
166  r |= (buf[i] & 0x000000ff);
167  }
168  else
169  for (int i = 0; i < buf.Length; i++) {
170  r <<= 8;
171  r |= (buf[i] & 0x000000ff);
172  }
173  return r;
174  }
175  public static long getLong(byte[] buf, bool asc) {
176  if (buf == null) {
177  //throw new IllegalArgumentException("byte array is null!");
178  }
179  if (buf.Length > 8) {
180  //throw new IllegalArgumentException("byte array size > 8 !");
181  }
182  long r = 0;
183  if (asc)
184  for (int i = buf.Length - 1; i >= 0; i--) {
185  r <<= 8;
186  r |= (buf[i] & (uint)0x00000000000000ff);
187  }
188  else
189  for (int i = 0; i < buf.Length; i++) {
190  r <<= 8;
191  r |= (buf[i] & (uint)0x00000000000000ff);
192  }
193  return r;
194  }
195  }
196 }
static byte[] getBytes(int s, bool asc)
Definition: TypeConvert.cs:100
static short getShort(byte[] buf, bool asc)
Definition: TypeConvert.cs:135
static int getInt(byte[] buf, bool asc)
Definition: TypeConvert.cs:155
static byte[] getBytes(long s, bool asc)
Definition: TypeConvert.cs:118
static long getLong(byte[] buf, bool asc)
Definition: TypeConvert.cs:175
static byte[] getBytes(short s, bool asc)
Definition: TypeConvert.cs:78