import android.text.TextUtils; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; public class FormatTransfer { /** * 将int转为低字节在前,高字节在后的byte数组 * * @param n int * @return byte[] */ public static byte[] toLH(int n) { byte[] b = new byte[4]; b[0] = (byte) (n & 0xff); b[1] = (byte) (n >> 8 & 0xff); b[2] = (byte) (n >> 16 & 0xff); b[3] = (byte) (n >> 24 & 0xff); return b; } 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[i] & 0xff)); } return result.toString(); } /** * 将低字节数组转换为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; } public static long bytes2Long(byte[] byteNum) { long num = 0; for (int ix = 0; ix < 8; ++ix) { num <<= 8; num |= (byteNum[ix] & 0xff); } return num; } public static int hBytesToInt(byte[] src, int offset) { int value; value = (int) ( ((src[offset] & 0xFF)<<24) |((src[offset+1] & 0xFF)<<16) |((src[offset+2] & 0xFF)<<8) |(src[offset+3] & 0xFF)); return value; } public static int getIntFromByteBuffer(ByteBuffer buf, int offset) { buf.position(offset); buf.limit(offset + 4); byte[] bytes = new byte[4]; buf.get(bytes); return lBytesToInt(bytes); } public static boolean getBooleanFromByteBuffer(ByteBuffer buf, int offset) { buf.position(offset); buf.limit(offset + 1); byte[] bytes = new byte[1]; buf.get(bytes); return (bytes[0] & 0x01)==1 ? true : false; } public static String getStringFromByteBuffer(ByteBuffer buf, int offset, int len) { buf.position(offset); buf.limit(offset + len); byte[] bytes = new byte[len]; buf.get(bytes); try { return new String(bytes, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public static byte[] getBytesFromByteBuffer(ByteBuffer buf, int offset, int len) { buf.position(offset); buf.limit(offset + len); byte[] bytes = new byte[len]; buf.get(bytes); return bytes; } static public byte[] getBytes(char[] chars) { byte[] buf = new byte[chars.length]; for (int i = 0; i < chars.length; i++) { buf[i] = (byte) chars[i]; } return buf; } static public void putStringToByteBuffer(ByteBuffer buf, String str, int maxLen) { if (TextUtils.isEmpty(str) || str.getBytes().length > maxLen) { buf.put(new byte[maxLen]); } else { buf.put(str.getBytes()); buf.put(new byte[maxLen - str.getBytes().length]); } } /** * int转为二进制 str * * @param int num * @return String */ public static String lIntToStrBinary(int num) { String str_Binary = ""; while (num != 0){ str_Binary = str_Binary + num % 2; num = num / 2; } if(str_Binary.length() < 7){ int len = str_Binary.length(); for(int i = 0; i < 7-len ; i ++) { str_Binary = str_Binary + "0"; } } return str_Binary; } public static short byteToShort(byte[] b) { short s = 0; short s0 = (short) (b[0] & 0xff);// 最低位 short s1 = (short) (b[1] & 0xff); s1 <<= 8; s = (short) (s0 | s1); return s; } }
大小端转换
最新推荐文章于 2024-05-19 05:45:00 发布