安卓中各种数据类型之间的转换
2017-04-26 16:15
423 查看
留着以后用,先备份起来。
public class BytesUtil { private static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F' }; public static int bytes2Integer(byte[] data, int offset, int len) { // TODO Auto-generated method stub if((data == null) || (offset < 0) || (len <= 0) || (len + offset > data.length)) { return -1; } int value = 0; for(int i = 0; i < len; i++) { value = value*256 + oneByte2Integer(data[i+offset]); } return value; } public static int ascii2Integer(byte[] data, int offset, int len) { // TODO Auto-generated method stub if((data == null) || (offset < 0) || (len <= 0) || (len + offset > data.length)) { return -1; } int value = 0; for(int i = 0; i < len; i++) { value = value*10 + oneByte2Integer(data[i+offset])-0x30; } return value; } public static int oneByte2Integer(byte b) { // TODO Auto-generated method stub return b < 0 ? (b + 256) : b; } public static int BCDtoInteger(byte[] buffer, int offset, int len) { // TODO Auto-generated method stub if((buffer == null) || (offset < 0) || (len <= 0) || (buffer.length < offset+len)) { return -2147483648; } int value = 0; for(int i = offset; i < len+offset; i++) { value = (value*10) + ((buffer[i] >>> 4) & 0x0F); value = (value*10) + (buffer[i] & 0x0F); } return value; } //private final static byte[] hex = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF};//"0123456789ABCDEF".getBytes(); // 从字节数组到十六进制字符串转换 public static final String bytesToHexStr(byte[] data,int begin,int len,String filling) { if((data == null) || (begin < 0) || (len <= 0) || (data.length < begin + len)) { return null; } StringBuffer buffer = new StringBuffer(len * 2); for (int i = begin; i < begin+len; i++) { buffer.append(hex[(data[i] >>> 4) & 0x0F]); buffer.append(hex[data[i] & 0x0F]); buffer.append(filling); } return buffer.toString(); } // 从字节数组到十六进制字符串转换 public static final String bytesToHexStr(byte[] bcd,int begin,int len) { if(bcd != null) { StringBuffer buffer = new StringBuffer(len * 2); for (int i = begin; i < begin+len; i++) { buffer.append(hex[(bcd[i] >>> 4) & 0x0F]); buffer.append(hex[bcd[i] & 0x0F]); } return buffer.toString(); } return null; } // 从字节数组到十六进制字符串转换 public static String bytesToHexStr(char[] bcd,int begin,int len) { if(bcd != null) { StringBuffer buffer = new StringBuffer(len * 2); for (int i = begin; i < begin+len; i++) { buffer.append(hex[(bcd[i] >>> 4) & 0x0F]); buffer.append(hex[bcd[i] & 0x0F]); } return buffer.toString(); } return null; } public static String byteToHexStr(byte b) { StringBuffer buffer = new StringBuffer(2); buffer.append(hex[(b >>> 4) & 0x0F]); buffer.append(hex[b & 0x0F]); return buffer.toString(); } public static String byteToHexStr(char b) { StringBuffer buffer = new StringBuffer(2); buffer.append(hex[(b >>> 4) & 0x0F]); buffer.append(hex[b & 0x0F]); return buffer.toString(); } // 从十六进制字符串到字节数组转换 public static byte[] HexStringToBytes(String hexstr) { byte[] b = new byte[hexstr.length() / 2]; int j = 0; for (int i = 0; i < b.length; i++) { char c0 = hexstr.charAt(j++); char c1 = hexstr.charAt(j++); b[i] = (byte) ((parse(c0) << 4) | parse(c1)); } return b; } private static int parse(char c) { if (c >= 'a') return (c - 'a' + 10) & 0x0f; if (c >= 'A') return (c - 'A' + 10) & 0x0f; return (c - '0') & 0x0f; } public static String bytesToString(byte[] data,int begin,int len,String code) { String str = null; if(data != null) { try { if(code != null) { str = new String(data,begin,len,code); } else { str = new String(data,begin,len); } } catch (NullPointerException e) { // TODO: handle exception e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); } } return str; } public static String bytesToHexStr(byte[] data, int begin, int len, int maxLen, String filling) { // TODO Auto-generated method stub if((data == null) || (begin < 0) || (len <= 0) || (data.length < begin + len) || (maxLen <= 0)) { return null; } StringBuffer buffer = new StringBuffer(len * 2); for (int i = begin; i < begin+len; i++) { buffer.append(hex[(data[i%maxLen] >>> 4) & 0x0F]); buffer.append(hex[data[i%maxLen] & 0x0F]); buffer.append(filling); } return buffer.toString(); } //把高位是拆字的字符数组整合 public static byte[] getbytesIntegrate(byte[] data, int sta, int end) throws IllegalArgumentException { // TODO Auto-generated method stub if((data == null) || (sta < 0) || (end < 0 )) { throw new IllegalArgumentException("error"); } byte[] bytesData=new byte[(end-sta)/2]; for(int i=0;i<bytesData.length;i++) { bytesData[i]=(byte) ((data[2*i+sta]&0x0F)*16+(data[2*i+sta+1]&0x0F)); } return bytesData; } public static int bytesIntegrateToInteger(byte[] data, int offset, int len) throws IllegalArgumentException { byte[] bytesData = getbytesIntegrate(data,offset,offset + len); if(bytesData != null) { return bytes2Integer(bytesData,0,bytesData.length); } return 0; } public static int getbytesIntegrate(byte[] out_data,int offset,byte[] data, int sta, int end,int max) throws IllegalArgumentException { // TODO Auto-generated method stub if((data == null) || (sta < 0) || (end < 0 ) || (out_data == null) || (offset < 0)) { throw new IllegalArgumentException("error"); } int len = (end - sta + max) % max; if(len > data.length) { throw new IllegalArgumentException("error"); } len = len / 2; if(offset + len > out_data.length) { throw new IllegalArgumentException("error"); } for(int i = 0;i < len;i++) { out_data[i+offset]=(byte) ((data[(2 * i + sta) % max] & 0x0F) * 16+(data[(2 * i + sta + 1) % max] & 0x0F)); } return len; } public static byte[] getBytesExtend(byte[] data) { // TODO Auto-generated method stub if(data == null) { return null; } byte[] bytesData=new byte[data.length*2]; int len = getBytesExtend(bytesData,0,data,0,data.length); if(len >= 0) { return bytesData; } else { return null; } } public static int getBytesExtend(byte[] out_data,int offset,byte[] data) { // TODO Auto-generated method stub if((out_data == null) || (data == null)) { return -1; } if((offset < 0) || (offset >= out_data.length)) { return -1; } if(out_data.length < offset+data.length*2) { return -1; } for(int i=0;i<data.length;i++) { out_data[2*i+offset]=(byte) (((oneByte2Integer(data[i]) >>> 4) & 0x0F)+0x30); out_data[2*i+1+offset]=(byte) ((oneByte2Integer(data[i]) & 0x0F)+0x30); } return data.length*2; } public static byte getHExtend(byte b) { // TODO Auto-generated method stub return (byte) (((b >>> 4) & 0x0F) + 0x30); } public static byte getLExtend(byte b) { // TODO Auto-generated method stub return (byte) ((b & 0x0F) + 0x30); } public static int getBytesExtend(byte[] out_data, int out_data_offset, byte[] data, int offset, int len) { // TODO Auto-generated method stub if((data == null) || (out_data == null) || (out_data_offset < 0) || (offset < 0) || (out_data.length < out_data_offset + 2 * len) || (data.length < offset + len)) { return -1; } for(int i = 0;i < len;i++) { ou 4000 t_data[2*i + out_data_offset]=(byte) (((oneByte2Integer(data[i + offset]) >>> 4) & 0x0F)+0x30); out_data[2*i + 1 + out_data_offset]=(byte) ((oneByte2Integer(data[i + offset]) & 0x0F)+0x30); } return 2 * len; } public static String bytesToHexStr(byte[] data) { // TODO Auto-generated method stub if(data != null) { return bytesToHexStr(data,0,data.length); } return null; } public static void setBytes(byte charType,byte[]dataBuf) { // TODO Auto-generated method stub if(dataBuf!=null) { int i=0; for(i=0;i<dataBuf.length;i++) dataBuf[i]=charType; } } // 十六进制转换字符串 public static String hexStr2Str(String hexStr) { String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return new String(bytes); }}
相关文章推荐
- OC各种数据类型之间的转换方法
- VC中各种数据类型之间的转换
- java各种数据类型之间的转换
- C++各种数据类型之间的相互转换
- OC各种数据类型之间的转换方法
- Object-c中各种数据类型之间的转换
- 在MFC中各种数据类型之间的转换
- 黑马程序员-进制转换优化(适合各种类型数据之间的转换)
- C++各种数据类型之间的转换
- 用标准C++进行string与各种内置类型数据之间的转换
- VC中各种类型的数据之间的转换
- 用标准C++进行string与各种内置类型数据之间的转换
- OC各种数据类型之间的转换方法
- VC中的各种数据类型之间的转换
- android中各种数据类型之间转换
- wxString与各种数据类型之间的转换(摘自:wiki.wxwidgets.org)
- QT中各种数据类型之间的转换
- Qt中各种数据类型之间的转换
- OC各种数据类型之间的转换方法
- OC各种数据类型之间的转换方法