|
Java与其他语言数据类型之间的转换方法(2) public static byte[] toHH(short n) { byte[] b = new byte[2]; b[1] = (byte) (n & 0xff); b[0] = (byte) (n >> 8 & 0xff); return b; }
/** * 将将int转为高字节在前,低字节在后的byte数组
public static byte[] toHH(int number) { int temp = number; byte[] b = new byte[4]; for (int i = b.length - 1; i > -1; i--) { b = new Integer(temp & 0xff).byteValue(); temp = temp >> 8; } return b; }
public static byte[] IntToByteArray(int i) { byte[] abyte0 = new byte[4]; abyte0[3] = (byte) (0xff & i); abyte0[2] = (byte) ((0xff00 & i) >> 8); abyte0[1] = (byte) ((0xff0000 & i) >> 16); abyte0[0] = (byte) ((0xff000000 & i) >> 24); return abyte0; }
*/
/** * 将float转为低字节在前,高字节在后的byte数组 */ public static byte[] toLH(float f) { return toLH(Float.floatToRawIntBits(f)); }
/** * 将float转为高字节在前,低字节在后的byte数组 */ public static byte[] toHH(float f) { return toHH(Float.floatToRawIntBits(f)); }
/** * 将String转为byte数组 */ public static byte[] stringToBytes(String s, int length) { while (s.getBytes().length < length) { s += " "; } 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(); }
/**
|