⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inttobytes.java

📁 MM7彩信对接网关示例
💻 JAVA
字号:
/*
 * Created on 2003-12-24
 * 
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.rainbow.util.tools;

/**
 * @author liuhongshan
 * 
 * 将整数转换成byte型数组,有big-endian,little-endian两种。
 */
public class IntToBytes {

    //将int转换成byte数组,大端字节序
    public static byte[] intToBytesB(int i) {

        byte[] buf = new byte[4];

        buf[0] = (byte) (((i >> 24) & 0x000000FF));
        buf[1] = (byte) (((i >> 16) & 0x000000FF));
        buf[2] = (byte) (((i >> 8) & 0x000000FF));
        buf[3] = (byte) (((i) & 0x000000FF));

        return buf;
    }

    //	将int转换成byte数组,小端字节序
    public static byte[] intToBytesL(int i) {

        byte[] buf = new byte[4];

        buf[0] = (byte) (((i) & 0x000000FF));
        buf[1] = (byte) (((i >> 8) & 0x000000FF));
        buf[2] = (byte) (((i >> 16) & 0x000000FF));
        buf[3] = (byte) (((i >> 24) & 0x000000FF));

        return buf;
    }

    //改变字节序int
    public static int alterEndianInt(int i) {

        int j = 0;
        
        j |= (i<<24)&0xFF000000;
        j |= (i<<8)&0x00FF0000;
        j |= (i>>8)&0x0000FF00;
        j |= (i>>24)&0x00000FF;

        return j;
    }
    
//  改变字节序short
    public static short alterEndianShort(short i) {

        short j = 0;
        
        j |= (i<<8)&0xFF00;
        j |= (i>>8)&0x00FF;

        return j;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -