• [ZZ]byte[]到short、int、long的相互转换


    public final static byte[] getBytes(short s, boolean asc) 
    {
    	byte[] buf = new byte[2];
    	if (asc)
    		for (int i = buf.length - 1; i >= 0; i--) 
    		{
    			buf[i] = (byte) (s & 0x00ff);
    		  s >>= 8;
    		}
    	else
    		for (int i = 0; i < buf.length; i++) 
    		{
    			buf[i] = (byte) (s & 0x00ff);
    		  s >>= 8;
    		}
    	return buf;
    }
    
    public final static byte[] getBytes(int s, boolean asc) 
    {
    	byte[] buf = new byte[4];
    	if (asc)
    	  for (int i = buf.length - 1; i >= 0; i--) 
    	  {
    	    buf[i] = (byte) (s & 0x000000ff);
    	    s >>= 8;
    	  }
    	else
    	  for (int i = 0; i < buf.length; i++)
    	  {
    	    buf[i] = (byte) (s & 0x000000ff);
    	    s >>= 8;
    	  }
    	return buf;
    }
    
    public final static byte[] getBytes(long s, boolean asc) 
    {
    	byte[] buf = new byte[8];
    	if (asc)
    	  for (int i = buf.length - 1; i >= 0; i--) 
    		{
    	    buf[i] = (byte) (s & 0x00000000000000ff);
    	    s >>= 8;
    	  }
    	else
    	  for (int i = 0; i < buf.length; i++) 
    		{
    	    buf[i] = (byte) (s & 0x00000000000000ff);
    	    s >>= 8;
    	  }
    	return buf;
    }
    
    public final static short getShort(byte[] buf, boolean asc) 
    {
    	if (buf == null) 
    	{
    	  throw new IllegalArgumentException("byte array is null!");
    	}
    	if (buf.length > 2) 
    	{
    	  throw new IllegalArgumentException("byte array size > 2 !");
    	}
    	short r = 0;
    	if (asc)
    	  for (int i = buf.length - 1; i >= 0; i--) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x00ff);
    	  }
    	else
    	  for (int i = 0; i < buf.length; i++) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x00ff);
    	  }
    	return r;
    }
    
    public final static int getInt(byte[] buf, boolean asc) 
    {
    	if (buf == null) 
    	{
    	  throw new IllegalArgumentException("byte array is null!");
    	}
    	if (buf.length > 4) 
    	{
    	  throw new IllegalArgumentException("byte array size > 4 !");
    	}
    	int r = 0;
    	if (asc)
    	  for (int i = buf.length - 1; i >= 0; i--) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x000000ff);
    	  }
    	else
    	  for (int i = 0; i < buf.length; i++) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x000000ff);
    	  }
    	return r;
    }
    
    public final static long getLong(byte[] buf, boolean asc) 
    {
    	if (buf == null) 
    	{
    	  throw new IllegalArgumentException("byte array is null!");
    	}
    	if (buf.length > 8) 
    	{
    	  throw new IllegalArgumentException("byte array size > 8 !");
    	}
    	long r = 0;
    	if (asc)
    	  for (int i = buf.length - 1; i >= 0; i--) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x00000000000000ff);
    	  }
    	else
    	  for (int i = 0; i < buf.length; i++) 
    		{
    	    r <<= 8;
    	    r |= (buf[i] & 0x00000000000000ff);
    	  }
    	return r;
    }
    

  • 相关阅读:
    iOS 代码让手机震动一下
    iOS开发 --制作圆形的头像(UIImage)
    JAVA基本数据类型和引用数据类型的区别
    jquery基础
    JS基础
    JAVA异常详解
    单例模式详解及java常用类
    JAVA基础之字符串和面向对象
    我的第一篇博客 初识动画,飞机行小动画
    GCD系列:调度组(dispatch_group)
  • 原文地址:https://www.cnblogs.com/pegasus923/p/1845866.html
Copyright © 2020-2023  润新知