zoukankan      html  css  js  c++  java
  • java int转byte数组

    int 转 byte[]   低字节在前(低字节序)

    1 public static byte[] toLH(int n) {  
    2   byte[] b = new byte[4];  
    3   b[0] = (byte) (n & 0xff);  
    4   b[1] = (byte) (n >> 8 & 0xff);  
    5   b[2] = (byte) (n >> 16 & 0xff);  
    6   b[3] = (byte) (n >> 24 & 0xff);  
    7   return b;  
    8 }

    int 转 byte[]   高字节在前(高字节序)

    1 public static byte[] toHH(int n) {  
    2   byte[] b = new byte[4];  
    3   b[3] = (byte) (n & 0xff);  
    4   b[2] = (byte) (n >> 8 & 0xff);  
    5   b[1] = (byte) (n >> 16 & 0xff);  
    6   b[0] = (byte) (n >> 24 & 0xff);  
    7   return b;  
    8 }

    byte[] 转 int 低字节在前(低字节序)

    1 public int toInt(byte[] b){
    2     int res = 0;
    3     for(int i=0;i<b.length;i++){
    4         res += (b[i] & 0xff) << (i*8);
    5     }
    6     return res;
    7 }

    byte[] 转 int 高字节在前(高字节序)

    1 public static int toInt(byte[] b){
    2     int res = 0;
    3     for(int i=0;i<b.length;i++){
    4         res += (b[i] & 0xff) << ((3-i)*8);
    5     }
    6     return res;
    7 }
  • 相关阅读:
    OpenGL3:先导篇 数据类型
    Linux开发:同步与异步
    前端面试题
    工具
    API和DLL
    CSS了一个浮动导航条
    AJAX背景技术介绍
    2014年8月18日17:02:53
    怎么增加照片的KB大小
    HTML5增加的几个新的标签
  • 原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/13434669.html
Copyright © 2011-2022 走看看