`
saluya
  • 浏览: 119071 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

整型数 和 网络字节序的 byte[] 数组之间的转换

 
阅读更多

public class ByteConvert {
    // 以下 是整型数 和 网络字节序的  byte[] 数组之间的转换
    public static byte[] longToBytes(long n) {
        byte[] b = new byte[8];
        b[7] = (byte) (n & 0xff);
        b[6] = (byte) (n >> 8  & 0xff);
        b[5] = (byte) (n >> 16 & 0xff);
        b[4] = (byte) (n >> 24 & 0xff);
        b[3] = (byte) (n >> 32 & 0xff);
        b[2] = (byte) (n >> 40 & 0xff);
        b[1] = (byte) (n >> 48 & 0xff);
        b[0] = (byte) (n >> 56 & 0xff);
        return b;
    }
    
    public static void longToBytes( long n, byte[] array, int offset ){
        array[7+offset] = (byte) (n & 0xff);
        array[6+offset] = (byte) (n >> 8 & 0xff);
        array[5+offset] = (byte) (n >> 16 & 0xff);
        array[4+offset] = (byte) (n >> 24 & 0xff);
        array[3+offset] = (byte) (n >> 32 & 0xff);
        array[2+offset] = (byte) (n >> 40 & 0xff);
        array[1+offset] = (byte) (n >> 48 & 0xff);
        array[0+offset] = (byte) (n >> 56 & 0xff);
    }
    
    public static long bytesToLong( byte[] array )
    {
        return ((((long) array[ 0] & 0xff) << 56)
              | (((long) array[ 1] & 0xff) << 48)
              | (((long) array[ 2] & 0xff) << 40)
              | (((long) array[ 3] & 0xff) << 32)
              | (((long) array[ 4] & 0xff) << 24)
              | (((long) array[ 5] & 0xff) << 16)
              | (((long) array[ 6] & 0xff) << 8) 
              | (((long) array[ 7] & 0xff) << 0));        
    }
    
    public static long bytesToLong( byte[] array, int offset )
    {
        return ((((long) array[offset + 0] & 0xff) << 56)
              | (((long) array[offset + 1] & 0xff) << 48)
              | (((long) array[offset + 2] & 0xff) << 40)
              | (((long) array[offset + 3] & 0xff) << 32)
              | (((long) array[offset + 4] & 0xff) << 24)
              | (((long) array[offset + 5] & 0xff) << 16)
              | (((long) array[offset + 6] & 0xff) << 8) 
              | (((long) array[offset + 7] & 0xff) << 0));            
    }
    
    public static byte[] intToBytes(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }
    
    public static void intToBytes( int n, byte[] array, int offset ){
        array[3+offset] = (byte) (n & 0xff);
        array[2+offset] = (byte) (n >> 8 & 0xff);
        array[1+offset] = (byte) (n >> 16 & 0xff);
        array[offset] = (byte) (n >> 24 & 0xff);
    }    

    public static int bytesToInt(byte b[]) {
        return    b[3] & 0xff 
               | (b[2] & 0xff) << 8 
               | (b[1] & 0xff) << 16
               | (b[0] & 0xff) << 24;
    }

    public static int bytesToInt(byte b[], int offset) {
        return    b[offset+3] & 0xff 
               | (b[offset+2] & 0xff) << 8 
               | (b[offset+1] & 0xff) << 16
               | (b[offset] & 0xff) << 24;
    }

    public static byte[] uintToBytes( long n )
    {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        
        return b;
    }

    public static void uintToBytes( long n, byte[] array, int offset ){
        array[3+offset] = (byte) (n );
        array[2+offset] = (byte) (n >> 8 & 0xff);
        array[1+offset] = (byte) (n >> 16 & 0xff);
        array[offset]   = (byte) (n >> 24 & 0xff);
    }

    public static long bytesToUint(byte[] array) {  
        return ((long) (array[3] & 0xff))  
             | ((long) (array[2] & 0xff)) << 8  
             | ((long) (array[1] & 0xff)) << 16  
             | ((long) (array[0] & 0xff)) << 24;  
    }

    public static long bytesToUint(byte[] array, int offset) {   
        return ((long) (array[offset+3] & 0xff))  
              | ((long) (array[offset+2] & 0xff)) << 8  
             | ((long) (array[offset+1] & 0xff)) << 16  
             | ((long) (array[offset]   & 0xff)) << 24;  
    }

    public static byte[] shortToBytes(short n) {
        byte[] b = new byte[2];
        b[1] = (byte) ( n       & 0xff);
        b[0] = (byte) ((n >> 8) & 0xff);
        return b;
    }
    
    public static void shortToBytes(short n, byte[] array, int offset ) {        
        array[offset+1] = (byte) ( n       & 0xff);
        array[offset] = (byte) ((n >> 8) & 0xff);
    }
    
    public static short bytesToShort(byte[] b){
        return (short)( b[1] & 0xff
                      |(b[0] & 0xff) << 8 ); 
    }    

    public static short bytesToShort(byte[] b, int offset){
        return (short)( b[offset+1] & 0xff
                      |(b[offset]    & 0xff) << 8 ); 
    }

    public static byte[] ushortToBytes(int n) {
        byte[] b = new byte[2];
        b[1] = (byte) ( n       & 0xff);
        b[0] = (byte) ((n >> 8) & 0xff);
        return b;
    }    

    public static void ushortToBytes(int n, byte[] array, int offset ) {
        array[offset+1] = (byte) ( n       & 0xff);
        array[offset] = (byte)   ((n >> 8) & 0xff);
    }

    public static int bytesToUshort(byte b[]) {
        return    b[1] & 0xff 
               | (b[0] & 0xff) << 8;
    }    

    public static int bytesToUshort(byte b[], int offset) {
        return    b[offset+1] & 0xff 
               | (b[offset]   & 0xff) << 8;
    }    

    public static byte[] ubyteToBytes( int n ){
        byte[] b = new byte[1];
        b[0] = (byte) (n & 0xff);
        return b;
    }

    public static void ubyteToBytes( int n, byte[] array, int offset ){
        array[0] = (byte) (n & 0xff);
    }

    public static int bytesToUbyte( byte[] array ){            
        return array[0] & 0xff;
    }        

    public static int bytesToUbyte( byte[] array, int offset ){            
        return array[offset] & 0xff;
    }    
    // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 
}

 
public class ByteConvertTest {
    
    public static String byte2Hex(byte[] buf) 
    {
        StringBuffer strbuf = new StringBuffer();
        strbuf.append("{");
        for (byte b : buf) 
        {
            if (b == 0) 
            {
                strbuf.append("00");
            } 
            else if (b == -1) 
            {
                strbuf.append("FF");
            } 
            else 
            {
                String str = Integer.toHexString(b).toUpperCase();
                // sb.append(a);
                if (str.length() == 8) 
                {
                    str = str.substring(6, 8);
                } 
                else if (str.length() < 2) 
                {
                    str = "0" + str;
                }
                strbuf.append(str);
            }
            strbuf.append(" ");
        }
        strbuf.append("}");
        return strbuf.toString();
    }    

    public static byte[] longToBytes(long n) {
        byte[] b = new byte[8];
        b[7] = (byte) (n & 0xff);
        b[6] = (byte) (n >> 8  & 0xff);
        b[5] = (byte) (n >> 16 & 0xff);
        b[4] = (byte) (n >> 24 & 0xff);
        b[3] = (byte) (n >> 32 & 0xff);
        b[2] = (byte) (n >> 40 & 0xff);
        b[1] = (byte) (n >> 48 & 0xff);
        b[0] = (byte) (n >> 56 & 0xff);
        return b;
    }

    public static long bytesToLong( byte[] array )
    {
        return ((((long) array[ 0] & 0xff) << 56)
              | (((long) array[ 1] & 0xff) << 48)
              | (((long) array[ 2] & 0xff) << 40)
              | (((long) array[ 3] & 0xff) << 32)
              | (((long) array[ 4] & 0xff) << 24)
              | (((long) array[ 5] & 0xff) << 16)
              | (((long) array[ 6] & 0xff) << 8) 
              | (((long) array[ 7] & 0xff) ));        
    }
    
    public static int bytesToInt(byte b[]) {
        return    b[3] & 0xff 
               | (b[2] & 0xff) << 8 
               | (b[1] & 0xff) << 16
               | (b[0] & 0xff) << 24;
    }

    public static long bytesToUint(byte[] array) {  
        return ((long) (array[3] & 0xff))  
             | ((long) (array[2] & 0xff)) << 8  
             | ((long) (array[1] & 0xff)) << 16  
             | ((long) (array[0] & 0xff)) << 24;  
    }

    public static byte[] uintToBytes( long n )
    {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        
        return b;
    }
    

    public static byte[] shortToBytes(short n) {
        byte[] b = new byte[2];
        b[1] = (byte) ( n       & 0xff);
        b[0] = (byte) ((n >> 8) & 0xff);
        return b;
    }
    
    public static short bytesToShort(byte[] b){
        return (short)( b[1] & 0xff
                      |(b[0] & 0xff) << 8 ); 
    }
    
    static void testShortConvert(){
        System.out.println("=================== short convert =============");
        System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2)));        
        System.out.print("println 0x11f2:");
        System.out.println((short)0x11f2);        
        System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2)));        
        System.out.print("println 0xf1f2:");
        System.out.println((short)0xf1f2);            
        System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):");
        System.out.println((short)bytesToShort(shortToBytes((short)0x11f2)));            
        System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):");
        System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2)));        
    }
    

    public static byte[] ushortToBytes(int n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) (n >> 8 & 0xff);
        return b;
    }
    

    public static int bytesToUshort(byte b[]) {
        return    b[1] & 0xff 
               | (b[0] & 0xff) << 8;
    }

    static void testUshortConvert(){
        System.out.println("=================== Ushort convert =============");
        System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2)));        
        System.out.print("println 0x11f2:");
        System.out.println(0x11f2);        
        System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2)));        
        System.out.print("println 0xf1f2:");
        System.out.println(0xf1f2);            
        System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):");
        System.out.println(bytesToUshort(ushortToBytes(0x11f2)));            
        System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):");
        System.out.println(bytesToUshort(ushortToBytes(0xf1f2)));        
    }
    
    public static byte[] ubyteToBytes( int n ){
        byte[] b = new byte[1];
        b[0] = (byte) (n & 0xff);
        return b;
    }

    public static int bytesToUbyte( byte[] array ){            
        return array[0] & 0xff;
    }    

    static void testUbyteConvert(){
        System.out.println("=================== Ubyte convert =============");
        System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112)));        
        System.out.print("println 0x1112:");
        System.out.println(0x1112);        
        System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2)));        
        System.out.print("println 0xf2:");
        System.out.println(0xf2);            
        System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):");
        System.out.println(bytesToUbyte(ubyteToBytes(0x1112)));            
        System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):");
        System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2)));        
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub        
        byte[] array = new byte[4];
        array[3] = (byte) 0xF4;
        array[2] = 0x13;
        array[1] = 0x12;
        array[0] = 0x11;
        
        System.out.println("=================== Integer bytes =============");
        
        System.out.println("the bytes is:"+byte2Hex(array) );
        System.out.print("println bytesToInt :");
        System.out.println( bytesToInt(array));
        System.out.printf("printf bytesToInt :%X\n", bytesToInt(array));
        
        System.out.println("=================== long bytes =============");
        byte[] longBytes = new byte[8];
        
        longBytes[7] = (byte) 0xf7;
        longBytes[6] = (byte) 0x16;
        longBytes[5] = (byte) 0xf5;
        longBytes[4] = (byte) 0x14;
        longBytes[3] = (byte) 0xf3;
        longBytes[2] = (byte) 0x12;
        longBytes[1] = (byte) 0xf1;
        longBytes[0] = (byte) 0x10;
        

        System.out.println( "the bytes is:"+byte2Hex(longBytes) );
        System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes));
        
        System.out.println("=================byte to long ================");
        
        byte b = (byte)0xf1;
        System.out.print("Println the byte:");
        System.out.println(b);
        System.out.printf("Printf the byte:%X\n",b);
        long l = b;
        System.out.print("Println byte to long:");
        System.out.println(l);
        System.out.printf("printf byte to long:%X\n",l);
        
        System.out.println("================= uint Bytes ================");
        
        byte[] uint = new byte[4];
        uint[3] = (byte) 0xf3;
        uint[2] = (byte) 0x12;
        uint[1] = (byte) 0xf1;
        uint[0] = (byte) 0xFF;
        
        System.out.println( "the bytes is:"+byte2Hex(uint) );
        System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint));
        System.out.print("Println bytesToUint:");
        System.out.println(bytesToUint(uint));
        System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)));
        
        System.out.println("===============Long Integer==============");        
        System.out.print("println 0x11f2f3f4f5f6f7f8l:");
        System.out.println(0x11f2f3f4f5f6f7f8l);        
        System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l);
        System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l)));
        // 注意,下面的这行,并不能获得正确的uint。
        System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l)));
        
        System.out.println("===============bytesToLong(longToBytes())==============");
        System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
        System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l)));
        
        testShortConvert();
        testUshortConvert();
        testUbyteConvert();
    }

}
 
分享到:
评论

相关推荐

    int类型和byte数组之间的转换

    写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。

    java数据类型转byte数组

    ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...

    Java整型数与网络字节序byte[]数组转换关系详解

    主要介绍了Java整型数与网络字节序byte[]数组转换关系,结合实例形式归纳整理了java整型数和网络字节序的byte[]之间转换的各种情况,需要的朋友可以参考下

    LabVIEW,字节数组至数值转换

    LabVIEW程序,功能:将4字节的unsigned char输入组合成1个32-bit int值,若输入字节数不等于4则报错。

    C#各种数据类型转换

    C#各种数据类型转换,字符串 转换 char数组,...将Base64字符串解码为普通字符串,图片 转换 byte数组,byte数组 转换 图片,ip 转换 长整形,长整形 转换 IP,将8位日期型整型数据转换为日期字符串数据,string型转换为bool型,

    Java基本类型与byte数组之间相互转换方法

    下面小编就为大家带来一篇Java基本类型与byte数组之间相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    字节和float转换小工具

    字节位数和float型转换小工具,可实现双向转换,占容小,速度快捷

    C# byte转为有符号整数实例

    C#开发,收到下位机串口数据(温度信息),可能是正数也可能是负数,如何转换?...补充知识:c# byte数组转换 8位有符号整数 16位有符号整数 32位有符号整数 byte数组 byte[] aa = new byte[] { 0xF8

    C#浮点数和16进制字节数互相转换

    该代码可以实现单精度浮点数和16进制字符之间的转换,包含源代码

    LabVIEW 四字节数据转浮点数

    通信接收四个字节转浮点小数和数据解包,测试成功,完美使用

    c# 加密和解密相关代码

    count 数组中用作数据的字节数 返回值 计算所得的哈希代码 说明:本实例用到了ComputeHash 方法的第一种重载形式。 设 计过程 (1)打开Visual Studio 2008 开发环境,新建一个Windows窗体应用程序,并将其命名为...

    标准MFC WinSock ActiveX控件开发实例(II)高级篇

    case 4://当指定该值为4时,当Date为BYTE数组时,将把一个BYTE转换成一个char传送 case 5://当指定该值为5时,当Date为短整型数组时,将把一个short转换成两个char传送 case 6://当指定该值为6时,当Date为浮点型数组...

    VBSCRIPT中文手册

    Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...

    vb Script参考文档

    Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...

    VBScript 语言参考

    Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...

    VBScript 语言参考中文手册CHM

    Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...

    VBSCRIP5 -ASP用法详解

    Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...

    Python内建模块struct实例详解

    本文研究的主要是Python内建模块struct的相关内容,具体如下...(1)struct.pack:用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。 (2

    Java精华(免费版)

    //main()的参数是string类型的数组,用来做为长,宽时,要转换成整型。 { int w=new Integer(args[0]).intValue(); int h=Integer.parseInt(args[1]); //int h=Integer.valueOf(args[1]).intValue(); //以上为三种将...

    〖程序设计基础〗练习题3及答案

    4.java语言中的逻辑变量可以和整型变量相互强制转换。 5.面向对象的软件开发方法用类把数据和基于数据的操作封装在一起,并且类之间可以存在继承关系。 6.方法可以没有返回值,或有一个返回值,也可以有多个返回值。...

Global site tag (gtag.js) - Google Analytics