zoukankan      html  css  js  c++  java
  • byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形int转换成byte数组,同时如何把一个长度为4的byte数组转换成int类型。

    方法一:

    public static byte[] intToByteArray(int i) {  
      byte[] result = new byte[4];  
      // 由高位到低位  
      result[0] = (byte) ((i >> 24) & 0xFF);  
      result[1] = (byte) ((i >> 16) & 0xFF);  
      result[2] = (byte) ((i >> 8) & 0xFF);  
      result[3] = (byte) (i & 0xFF);  
      return result;  
    }  
    public static int byteArrayToInt(byte[] bytes) {  
        int value = 0;  
        // 由高位到低位  
        for (int i = 0; i < 4; i++) {  
          int shift = (4 - 1 - i) * 8;  
          value += (bytes[i] & 0x000000FF) << shift;// 往高位游  
        }  
        return value;  
    } 

    方法二:

    此方法可以对string类型,float类型,char类型等 来与 byte类型的转换

    public static void main(String[] args) {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        DataOutputStream dos = new DataOutputStream(baos);  
        try {  
            // int转换byte数组  
            dos.writeByte(-12);  
            dos.writeLong(12);  
            dos.writeChar('1');  
            dos.writeFloat(1.01f);  
            dos.writeUTF("好");  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
      
        byte[] aa = baos.toByteArray();  
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  
        DataInputStream dis = new DataInputStream(bais);  
        // byte转换int  
        try {  
             System.out.println(dis.readByte());  
             System.out.println(dis.readLong());  
             System.out.println(dis.readChar());  
             System.out.println(dis.readFloat());  
             System.out.println(dis.readUTF());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            dos.close();  
            dis.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
    public class RandomUtil {
      public static byte[] intToByteArray(int i) {  
          byte[] result = new byte[4];  
          // 由高位到低位  
          result[0] = (byte) ((i >> 24) & 0xFF);  
          result[1] = (byte) ((i >> 16) & 0xFF);  
          result[2] = (byte) ((i >> 8) & 0xFF);  
          result[3] = (byte) (i & 0xFF);  
          return result;  
      }  
    
      public static void main(String[] args) {
         int v = 123456;
        byte[] bytes = ByteBuffer.allocate(4).putInt(v).array();
         for (byte t : bytes) {
            System.out.println(t);
         }
        System.out.println("----- 分割线 -------");
        byte[] bytes2 = intToByteArray(v);
        for (byte t : bytes2) {
          System.out.println(t);
        }
      }
    }
  • 相关阅读:
    BZOJ 5358 口算训练/HDU 6287(可持久化线段树)
    HDU 4288 Coder
    FZU 2289 项链
    jQuery
    HTML5存储技术Storage
    JS第三部分--BOM浏览器对象模型
    JS第二部分--DOM文档对象模型
    JS第一部分--ECMAScript5.0标准语法 (JS基础语法)
    python离线安装包
    DB2不记日志插入,python迭代器操作xlrd,python操作xlwt
  • 原文地址:https://www.cnblogs.com/cherish010/p/9963337.html
Copyright © 2011-2022 走看看