zoukankan      html  css  js  c++  java
  • JAVA byte[], 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;
      }
  • 相关阅读:
    Effective Java 第三版——72. 赞成使用标准异常
    Effective Java 第三版——71. 避免不必要地使用检查异常
    Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常
    Effective Java 第三版——69. 仅在发生异常的条件下使用异常
    Effective Java 第三版——68. 遵守普遍接受的命名约定
    Effective Java 第三版——67. 明智谨慎地进行优化
    Effective Java 第三版——66. 明智谨慎地使用本地方法
    Effective Java 第三版——65. 接口优于反射
    Effective Java 第三版——64. 通过对象的接口引用对象
    Effective Java 第三版——63. 注意字符串连接的性能
  • 原文地址:https://www.cnblogs.com/super119/p/1924542.html
Copyright © 2011-2022 走看看