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);
    }
  }
}

byte数组和int之间相互转化的方法的更多相关文章

  1. byte[]数组和int之间的转换

    这里简单记录下两种转换方式: 第一种: 1.int与byte[]之间的转换(类似的byte short,long型) /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高 ...

  2. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  3. byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化

    我们先来看看byte bool  int ushort  等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...

  4. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  5. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  6. java byte数组与int,long,short,byte转换

    public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...

  7. byte数组与int,long,short,byte转换 (转载)

    byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...

  8. 深入 JAVA里面关于byte数组和String之间的转换问题

    把byte转化成string,必须经过编码.  例如下面一个例子:  importjava.io.UnsupportedEncodingException; publicclass test{ pub ...

  9. java中byte数组与int,long,short间的转换

    http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...

随机推荐

  1. 【洛谷P3917】异或序列

    题目大意:给定一个长度为 N 的序列,每个位置有一个权值,求 \[\sum\limits_{1\le i\le j\le n}(a_i\oplus a_{i+1}...\oplus a_j)\] 的值 ...

  2. Django(十一)请求生命周期之CBV与FBV

    https://www.cnblogs.com/yuanchenqi/articles/8715364.html FBV FBV(function base views) 就是在视图里使用函数处理请求 ...

  3. 怎么理解本征无序态的蛋白质(Intrinsically disordered proteins)

    见维基的解释: An intrinsically disordered protein (IDP) is a protein that lacks a fixed or ordered three-d ...

  4. mybatis 的批量更新操作sql

    转: mybatis 的批量更新操作sql 2018年07月23日 10:38:19 海力布 阅读数:1689   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  5. request鉴权的处理和判断

    一般查看蝉道bug管理工具bug列表的时候,会提示 Unauthorized  access,那是因为需要用户名和密码,一般用基本的认证,代码如下: 不是所有的系统都是开放的,有些人是不可以访问的,所 ...

  6. Image.fromarray的用法

    简而言之,就是实现array到image的转换 详细参考以下博客 https://blog.csdn.net/ybcrazy/article/details/81206411

  7. 0基础如何学Android开发

    链接:http://pan.baidu.com/s/1bIEIse 密码:ky7w https://pan.baidu.com/s/1i53bs6x提取码:0pwthttps://www.zhihu. ...

  8. toString()和toLocaleString()有什么区别

    偶然之间用到这两个方法 然后在数字转换成字符串的时候,并没有感觉这两个方法有什么区别,如下: 1 2 3 4 5 6 7 8 var e=123     e.toString() "123& ...

  9. java 集合的总结

    集合大致可以分为两类: 一类继承Collection接口,存储的是多个孤立的元素,包括List和set: List包括ArrayList类和LinkedList类,ArrayList数组的顺序存储,而 ...

  10. R语言模块安装

    一.ggplot2 install.pacakges("ggplot2")