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

Android游戏中其他语言数据类型之间的转换方法Java与其他语言数据类型之间的转换方法实例程序 ? }? return

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

Java与其他语言数据类型之间的转换方法实例程序

? }
? return s.getBytes();
}


/**
? * 将字节数组转换为String
? * @param b byte[]
? * @return String
? */
public static String bytesToString(byte[] b) {
? StringBuffer result = new StringBuffer("");
? int length = b.length;
? for (int i=0; i<length; i++) {
??? result.append((char)(b & 0xff));
? }
? return result.toString();
}

/**
? * 将字符串转换为byte数组
? * @param s String
? * @return byte[]
? */
public static byte[] stringToBytes(String s) {
? return s.getBytes();
}

/**
? * 将高字节数组转换为int
? * @param b byte[]
? * @return int
? */
public static int hBytesToInt(byte[] b) {
? int s = 0;
? for (int i = 0; i < 3; i++) {
??? if (b >= 0) {
??? s = s + b;
??? } else {
??? s = s + 256 + b;
??? }
??? s = s * 256;
? }
? if (b[3] >= 0) {
??? s = s + b[3];
? } else {
??? s = s + 256 + b[3];
? }
? return s;
}

/**
? * 将低字节数组转换为int
? * @param b byte[]
? * @return int
? */
public static int lBytesToInt(byte[] b) {
? int s = 0;
? for (int i = 0; i < 3; i++) {
??? if (b[3-i] >= 0) {
??? s = s + b[3-i];
??? } else {
??? s = s + 256 + b[3-i];
??? }
??? s = s * 256;
? }
? if (b[0] >= 0) {
??? s = s + b[0];
? } else {
??? s = s + 256 + b[0];
? }
? return s;
}


/**
? * 高字节数组到short的转换
? * @param b byte[]
? * @return short
? */
public static short hBytesToShort(byte[] b) {
? int s = 0;
? if (b[0] >= 0) {
??? s = s + b[0];
??? } else {
??? s = s + 256 + b[0];
??? }
??? s = s * 256;
? if (b[1] >= 0) {
??? s = s + b[1];
? } else {
??? s = s + 256 + b[1];
? }
? short result = (short)s;
? return result;
}

/**
? * 低字节数组到short的转换
? * @param b byte[]
? * @return short
? */
public static short lBytesToShort(byte[] b) {
? int s = 0;
? if (b[1] >= 0) {
??? s = s + b[1];
??? } else {
??? s = s + 256 + b[1];
??? }
??? s = s * 256;
? if (b[0] >= 0) {
??? s = s + b[0];
? } else {
??? s = s + 256 + b[0];
? }
? short result = (short)s;
? return result;
}

/**
? * 高字节数组转换为float
? * @param b byte[]
? * @return float
? */
public static float hBytesToFloat(byte[] b) {
? int i = 0;
? Float F = new Float(0.0);
? i = ((((b[0]&0xff)<<8 | (b[1]&0xff))<<8) | (b[2]&0xff))<<8 | (b[3]&0xff);
? return F.intBitsToFloat(i);
}

/**
? * 低字节数组转换为float
? * @param b byte[]
? * @return float
? */
public static float lBytesToFloat(byte[] b) {
? int i = 0;
? Float F = new Float(0.0);
? i = ((((b[3]&0xff)<<8 | (b[2]&0xff))<<8) | (b[1]&0xff))<<8 | (b[0]&0xff);
? return F.intBitsToFloat(i);
}

/**
? * 将byte数组中的元素倒序排列
? */
public static byte[] bytesReverseOrder(byte[] b) {
? int length = b.length;
? byte[] result = new byte[length];
? for(int i=0; i<length; i++) {
??? result[length-i-1] = b;
? }
? return result;
}

/**
? * 打印byte数组
? */
public static void printBytes(byte[] bb) {
? int length = bb.length;
? for (int i=0; i<length; i++) {
??? System.out.print(bb + " ");
? }
? System.out.println("");
}

public static void logBytes(byte[] bb) {
? int length = bb.length;
? String out = "";
? for (int i=0; i<length; i++) {
??? out = out + bb + " ";
? }

}

/**
? * 将int类型的值转换为字节序颠倒过来对应的int值
? * @param i int
? * @return int
? */
public static int reverseInt(int i) {
? int result = FormatTransfer.hBytesToInt(FormatTransfer.toLH(i));
? return result;
}

/**
? * 将short类型的值转换为字节序颠倒过来对应的short值
? * @param s short
? * @return short
? */
public static short reverseShort(short s) {
? short result = FormatTransfer.hBytesToShort(FormatTransfer.toLH(s));
? return result;
}

/**
? * 将float类型的值转换为字节序颠倒过来对应的float值
? * @param f float
? * @return float
? */
public static float reverseFloat(float f) {
? float result = FormatTransfer.hBytesToFloat(FormatTransfer.toLH(f));
? return result;
}

}