java处理业务能力强的工具类_java处理字节的常用工具类

java处理字节的常用工具类

发布于 2020-12-29|

复制链接

摘记: 处理字节的常用工具类方法,供大家参考,具体内容如下

```java

package com.demo.utils;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import jav ..

处理字节的常用工具类方法,供大家参考,具体内容如下

```java

package com.demo.utils;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.nio.charset.Charset;

import java.util.Arrays;

/**

* 处理字节的常用工具类方法

* @author dongyangyang

* @Date 2017/3/13 12:31

* @Version 1.0

*

*/

public class ByteUtils {

/**

* 构造新字节时需要与的值表

*/

private static final byte[] BUILD_BYTE_TABLE = new byte[] { (byte) 128, (byte) 64, (byte) 32, (byte) 16, (byte) 8, (byte) 4, (byte) 2, (byte) 1 };

private ByteUtils() {}

/**

* short转换到字节数组

*

* @param number

* 需要转换的数据。

* @return 转换后的字节数组。

*/

public static byte[] shortToByte(short number) {

byte[] b = new byte[2];

for (int i = 1; i >= 0; i--) {

b[i] = (byte) (number % 256);

number >>= 8;

}

return b;

}

/**

* 字节到short转换

*

* @param b

* short的字节数组

* @return short数值。

*/

public static short byteToShort(byte[] b) {

return (short) ((((b[0] & 0xff) = 0; i--) {

b[i] = (byte) (number % 256);

number >>= 8;

}

return b;

}

/**

* 字节数组到整型转换

*

* @param b

* 整形数据的字节数组。

* @return 字节数组转换成的整形数据。

*/

public static int byteToInt(byte[] b) {

return ((((b[0] & 0xff) = 0; i--) {

b[i] = (byte) (number % 256);

number >>= 8;

}

return b;

}

/**

* 字节数组到整型的转换

*

* @param b

* 长整形字节数组。

* @return 长整形数据。

*/

public static long byteToLong(byte[] b) {

return ((((long) b[0] & 0xff) > 8;

}

return bytes;

}

/**

* 字节数组到double转换

*

* @param b

* 双精度浮点字节数组。

* @return 双精度浮点数据。

*/

public static double byteToDouble(byte[] b) {

long l;

l = b[0];

l &= 0xff;

l |= ((long) b[1] > 8;

}

return bytes;

}

/**

* 字节数组到float的转换

*

* @param b

* 浮点型数据字节数组。

* @return 浮点型数据。

*/

public static float byteToFloat(byte[] b) {

int l;

l = b[0];

l &= 0xff;

l |= ((long) b[1] = 0; i--) {

buff[index++] = ((b >>> i) & 1) == 1;

}

return buff;

}

/**

* 返回指定字节中指定bit位,true为1,false为0. 指定的位从0-7,超出将抛出数据越界异常.

*

* @param b

* 需要判断的字节.

* @param index

* 字节中指定位.

* @return 指定位的值.

*/

public static boolean byteBitValue(byte b, int index) {

return byteToBitArray(b)[index];

}

/**

* 根据布尔数组表示的二进制构造一个新的字节.

*

* @param values

* 布尔数组,其中true表示为1,false表示为0.

* @return 构造的新字节.

*/

public static byte buildNewByte(boolean[] values) {

byte b = 0;

for (int i = 0; i 15) {

throw new IllegalArgumentException("Invalid IP address.");

}

final int ipSize = 4;// 最大IP位数

final char ipSpace = '.';// IP数字的分隔符

int[] ipNums = new int[ipSize];

StringBuilder number = new StringBuilder();// 当前操作的数字

StringBuilder buff = new StringBuilder(address);

int point = 0;// 当前操作的数字下标,最大到3.

char currentChar;

for (int i = 0; i 0) {

throw new IllegalArgumentException("Invalid IP address.");

}

ipNums[point++] = Integer.parseInt(number.toString());

number.delete(0, number.length());

} else {

number.append(currentChar);

}

}

ipNums[point] = Integer.parseInt(number.toString());

byte[] ipBuff = new byte[ipSize];

int pointNum = 0;

for (int i = 0; i 255) {

throw new IllegalArgumentException("Invalid IP address.");

}

ipBuff[i] = (byte) (pointNum & 0xff);

}

return ipBuff;

}

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值