java 代码
- package com.sunhill.k1c.sms.util;
- import java.nio.*;
- //数据转换
- /**
- *create by Shane about dataconvert
- *PCCW BJ DEV
- */
- public class DataConvertUtil {
- public static int byte2int(byte[] b){
- return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24;
- }
- public static byte[] int2Byte(int value){
- byte[] b=new byte[4];
- ByteBuffer buffer = ByteBuffer.allocate(4);
- buffer.asIntBuffer().put(value);
- for(int i=0;i<4;i++){
- b[i]= buffer.get();
- }
- return b;
- }
- public static long byte2uint(byte b[]) {
- return (long)b[3] & (long)255 | ((long)b[2] & (long)255) << 8 | ((long)b[1] & (long)255) << 16 | ((long)b[0] & (long)255) << 24;
- }
- public static byte[] uint2byte(long value) {
- byte b[] = new byte[4];
- b[0] = (byte)(int)(value >> 24);
- b[1] = (byte)(int)(value >> 16);
- b[2] = (byte)(int)(value >> 8);
- b[3] = (byte)(int)value;
- return b;
- }
- public static String byte2String(byte[] b){
- return new String(b);
- }
- public static byte[] getSubByte(byte[] buffer,int from,int len){
- if(from+len>buffer.length){
- len=buffer.length-from;
- }
- byte[] b = new byte[len];
- for(int i=0;i<len;i++){
- b[i]= buffer[i+from];
- }
- return b;
- }
- public static void setSubByte(byte[] dest, byte[] src,int from,int len){
- if(dest==null){
- return;
- }
- if(from+len>dest.length){
- return;
- }
- if(src!=null){
- for(int i=0;i<len;i++){
- if(i>=src.length){
- dest[i+from]=0;
- }else{
- dest[i+from]=src[i];
- }
- }
- }else{
- for(int i=0;i<len;i++){
- dest[i+from]=0;
- }
- }
- }
- }