Android游戏中其他语言数据类型之间的换方法

    java与其他语言数据类型之间的转换方法实例程序
<div class="code">/**<br>* 通信格式转换<br>*<br>* java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换<br>* 高、低字节之间的转换<br>* windows的字节序为低字节开头<br>* linux,unix的字节序为高字节开头<br>* java则无论平台变化,都是高字节开头<br>  */
public class formattransfer {<br>/**<br>  * 将int转为低字节在前,高字节在后的byte数组<br>  * @param n int<br>  * @return byte[]<br>  */<br>public static byte[] tolh(int n) {<br>  byte[] b = new byte[4];<br>  b[0] = (byte) (n &amp; 0xff);<br>  b[1] = (byte) (n >> 8 &amp; 0xff);<br>  b[2] = (byte) (n >> 16 &amp; 0xff);<br>  b[3] = (byte) (n >> 24 &amp; 0xff);<br>  return b;<br>}
/**<br>  * 将int转为高字节在前,低字节在后的byte数组<br>  * @param n int<br>  * @return byte[]<br>  */<br>public static byte[] tohh(int n) {<br>  byte[] b = new byte[4];<br>  b[3] = (byte) (n &amp; 0xff);<br>  b[2] = (byte) (n >> 8 &amp; 0xff);<br>  b[1] = (byte) (n >> 16 &amp; 0xff);<br>  b[0] = (byte) (n >> 24 &amp; 0xff);<br>  return b;<br>}
/**<br>  * 将short转为低字节在前,高字节在后的byte数组<br>  * @param n short<br>  * @return byte[]<br>  */<br>public static byte[] tolh(short n) {<br>  byte[] b = new byte[2];<br>  b[0] = (byte) (n &amp; 0xff);<br>  b[1] = (byte) (n >> 8 &amp; 0xff);<br>  return b;<br>}
/**<br>  * 将short转为高字节在前,低字节在后的byte数组<br>  * @param n short<br>  * @return byte[]<br>  */<br>public static byte[] tohh(short n) {<br>  byte[] b = new byte[2];<br>  b[1] = (byte) (n &amp; 0xff);<br>  b[0] = (byte) (n >> 8 &amp; 0xff);<br>  return b;<br>}
 
/**<br>  * 将将int转为高字节在前,低字节在后的byte数组
public static byte[] tohh(int number) {<br>  int temp = number;<br>  byte[] b = new byte[4];<br>  for (int i = b.length - 1; i > -1; i--) {<br>    b = new integer(temp &amp; 0xff).bytevalue();<br>    temp = temp >> 8;<br>  }<br>  return b;<br>}
public static byte[] inttobytearray(int i) {<br>    byte[] abyte0 = new byte[4];<br>    abyte0[3] = (byte) (0xff &amp; i);<br>    abyte0[2] = (byte) ((0xff00 &amp; i) >> ;<br>    abyte0[1] = (byte) ((0xff0000 &amp; i) >> 16);<br>    abyte0[0] = (byte) ((0xff000000 &amp; i) >> 24);<br>    return abyte0;<br>}
<br>*/
/**<br>  * 将float转为低字节在前,高字节在后的byte数组<br>  */<br>public static byte[] tolh(float f) {<br>  return tolh(float.floattorawintbits(f));<br>}
/**<br>  * 将float转为高字节在前,低字节在后的byte数组<br>  */<br>public static byte[] tohh(float f) {<br>  return tohh(float.floattorawintbits(f));<br>}
/**<br>  * 将string转为byte数组<br>  */<br>public static byte[] stringtobytes(string s, int length) {<br>  while (s.getbytes().length < length) {<br>    s += " ";<br>  }<br>  return s.getbytes();<br>}
<br>/**<br>  * 将字节数组转换为string<br>  * @param b byte[]<br>  * @return string<br>  */<br>public static string bytestostring(byte[] b) {<br>  stringbuffer result = new stringbuffer("");<br>  int length = b.length;<br>  for (int i=0; i<length; i++) {<br>    result.append((char)(b &amp; 0xff));<br>  }<br>  return result.tostring();<br>}
/**<br>  * 将字符串转换为byte数组<br>  * @param s string<br>  * @return byte[]<br>  */<br>public static byte[] stringtobytes(string s) {<br>  return s.getbytes();<br>}
/**<br>  * 将高字节数组转换为int<br>  * @param b byte[]<br>  * @return int<br>  */<br>public static int hbytestoint(byte[] b) {<br>  int s = 0;<br>  for (int i = 0; i < 3; i++) {<br>    if (b >= 0) {<br>    s = s + b;<br>    } else {<br>    s = s + 256 + b;<br>    }<br>    s = s * 256;<br>  }<br>  if (b[3] >= 0) {<br>    s = s + b[3];<br>  } else {<br>    s = s + 256 + b[3];<br>  }<br>  return s;<br>}
/**<br>  * 将低字节数组转换为int<br>  * @param b byte[]<br>  * @return int<br>  */<br>public static int lbytestoint(byte[] b) {<br>  int s = 0;<br>  for (int i = 0; i < 3; i++) {<br>    if (b[3-i] >= 0) {<br>    s = s + b[3-i];<br>    } else {<br>    s = s + 256 + b[3-i];<br>    }<br>    s = s * 256;<br>  }<br>  if (b[0] >= 0) {<br>    s = s + b[0];<br>  } else {<br>    s = s + 256 + b[0];<br>  }<br>  return s;<br>}
<br>/**<br>  * 高字节数组到short的转换<br>  * @param b byte[]<br>  * @return short<br>  */<br>public static short hbytestoshort(byte[] b) {<br>  int s = 0;<br>  if (b[0] >= 0) {<br>    s = s + b[0];<br>    } else {<br>    s = s + 256 + b[0];<br>    }<br>    s = s * 256;<br>  if (b[1] >= 0) {<br>    s = s + b[1];<br>  } else {<br>    s = s + 256 + b[1];<br>  }<br>  short result = (short)s;<br>  return result;<br>}
/**<br>  * 低字节数组到short的转换<br>  * @param b byte[]<br>  * @return short<br>  */<br>public static short lbytestoshort(byte[] b) {<br>  int s = 0;<br>  if (b[1] >= 0) {<br>    s = s + b[1];<br>    } else {<br>    s = s + 256 + b[1];<br>    }<br>    s = s * 256;<br>  if (b[0] >= 0) {<br>    s = s + b[0];<br>  } else {<br>    s = s + 256 + b[0];<br>  }<br>  short result = (short)s;<br>  return result;<br>}
/**<br>  * 高字节数组转换为float<br>  * @param b byte[]<br>  * @return float<br>  */<br>public static float hbytestofloat(byte[] b) {<br>  int i = 0;<br>  float f = new float(0.0);<br>  i = ((((b[0]&amp;0xff)<<8 | (b[1]&amp;0xff))<<8) | (b[2]&amp;0xff))<<8 | (b[3]&amp;0xff);<br>  return f.intbitstofloat(i);<br>}
/**<br>  * 低字节数组转换为float<br>  * @param b byte[]<br>  * @return float<br>  */<br>public static float lbytestofloat(byte[] b) {<br>  int i = 0;<br>  float f = new float(0.0);<br>  i = ((((b[3]&amp;0xff)<<8 | (b[2]&amp;0xff))<<8) | (b[1]&amp;0xff))<<8 | (b[0]&amp;0xff);<br>  return f.intbitstofloat(i);<br>}
/**<br>  * 将byte数组中的元素倒序排列<br>  */<br>public static byte[] bytesreverseorder(byte[] b) {<br>  int length = b.length;<br>  byte[] result = new byte[length];<br>  for(int i=0; i<length; i++) {<br>    result[length-i-1] = b;<br>  }<br>  return result;<br>}
/**<br>  * 打印byte数组<br>  */<br>public static void printbytes(byte[] bb) {<br>  int length = bb.length;<br>  for (int i=0; i<length; i++) {<br>    system.out.print(bb + " ");<br>  }<br>  system.out.println("");<br>}
public static void logbytes(byte[] bb) {<br>  int length = bb.length;<br>  string out = "";<br>  for (int i=0; i<length; i++) {<br>    out = out + bb + " ";<br>  }
}
/**<br>  * 将int类型的值转换为字节序颠倒过来对应的int值<br>  * @param i int<br>  * @return int<br>  */<br>public static int reverseint(int i) {<br>  int result = formattransfer.hbytestoint(formattransfer.tolh(i));<br>  return result;<br>}
/**<br>  * 将short类型的值转换为字节序颠倒过来对应的short值<br>  * @param s short<br>  * @return short<br>  */<br>public static short reverseshort(short s) {<br>  short result = formattransfer.hbytestoshort(formattransfer.tolh(s));<br>  return result;<br>}
/**<br>  * 将float类型的值转换为字节序颠倒过来对应的float值<br>  * @param f float<br>  * @return float<br>  */<br>public static float reversefloat(float f) {<br>  float result = formattransfer.hbytestofloat(formattransfer.tolh(f));<br>  return result;<br>}
}

你可能感兴趣的:(java,工作)